WPF DataGrid 单元格前景绑定不起作用
WPF DataGrid cell foreground binding not working
我正在尝试根据其值更改列的前景色。当我尝试静态设置前景时它可以工作但是当我使用转换器绑定时没有任何反应。
这是一个小例子
<DataGrid Foreground="White" FontSize="13" x:Name="datagrid_results" AutoGenerateColumns="False" ItemsSource="{Binding DataGridTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="3,5,1.6,35" MaxHeight="260">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Width="*" Header="Minimum" Binding="{Binding Minimum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn IsReadOnly="True" Width="*" Header="Maximum" Binding="{Binding Maximum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn IsReadOnly="True" Width="100" Header="Pass-Fail" Binding="{Binding Pass_Fail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding Pass_Fail , Converter={StaticResource s2b}}" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
这是我的转换器:
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Brush myBrush = Brushes.White;
string input = value as string;
switch (input)
{
case "Pass":
myBrush = Brushes.LightGreen;
break;
case "Fail":
myBrush = Brushes.Red;
break;
default:
myBrush = Brushes.White;
break;
}
return myBrush;
}
奇怪的是,在调试这个转换器时,刷子返回了正确的值。只是单元格没有改变其文本颜色。然而,当我使用这个时:<Setter Property="Foreground" Value="Red" />
我的单元格改变了它们的文本颜色。有什么我可能遗漏的吗?
静态颜色:
使用动态颜色:
我已经习惯了让事情正常进行,即使它不是我这样做的方式,如果你需要它快速工作,我可以为你提供一个替代方案,同时完美的解决方案即将到来。
为了使事情正常进行,我只使用了一个布尔值和触发器。
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding pass}" Value="true">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding pass}" Value="false">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
所以在这个例子中(我没有修改太多),我根据传递值(它只是一个布尔值)改变背景颜色。
>
DataGrid Foreground="White" FontSize="13" x:Name="datagrid_results"
AutoGenerateColumns="False" ItemsSource="{Binding ItemsClass,
UpdateSourceTrigger=PropertyChanged}" Margin="3,5,1.6,35"
MaxHeight="260">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Width="*" Header="Minimum" Binding="{Binding Minimum, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn IsReadOnly="True" Width="*" Header="Maximum" Binding="{Binding Maximum, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn IsReadOnly="True" Width="100" Header="Pass-Fail" Binding="{Binding Pass_Fail, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding Pass_Fail , Converter={StaticResource s2b}}" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns> </DataGrid>
从 xaml
的数据网格第一行中删除 Mode=TwoWay
public partial class MainWindow : Window
{
List<ItemsClass> items = new List<ItemsClass>();
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
items.Add(new ItemsClass()
{
Pass_Fail="Pass"
});
items.Add(new ItemsClass()
{
Pass_Fail = "Fail"
});
items.Add(new ItemsClass()
{
Pass_Fail = "Pass"
});
items.Add(new ItemsClass()
{
Pass_Fail = "Fail"
});
datagrid.ItemsSource = items;
}
}
我正在尝试根据其值更改列的前景色。当我尝试静态设置前景时它可以工作但是当我使用转换器绑定时没有任何反应。
这是一个小例子
<DataGrid Foreground="White" FontSize="13" x:Name="datagrid_results" AutoGenerateColumns="False" ItemsSource="{Binding DataGridTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="3,5,1.6,35" MaxHeight="260">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Width="*" Header="Minimum" Binding="{Binding Minimum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn IsReadOnly="True" Width="*" Header="Maximum" Binding="{Binding Maximum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn IsReadOnly="True" Width="100" Header="Pass-Fail" Binding="{Binding Pass_Fail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding Pass_Fail , Converter={StaticResource s2b}}" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
这是我的转换器:
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Brush myBrush = Brushes.White;
string input = value as string;
switch (input)
{
case "Pass":
myBrush = Brushes.LightGreen;
break;
case "Fail":
myBrush = Brushes.Red;
break;
default:
myBrush = Brushes.White;
break;
}
return myBrush;
}
奇怪的是,在调试这个转换器时,刷子返回了正确的值。只是单元格没有改变其文本颜色。然而,当我使用这个时:<Setter Property="Foreground" Value="Red" />
我的单元格改变了它们的文本颜色。有什么我可能遗漏的吗?
静态颜色:
使用动态颜色:
我已经习惯了让事情正常进行,即使它不是我这样做的方式,如果你需要它快速工作,我可以为你提供一个替代方案,同时完美的解决方案即将到来。 为了使事情正常进行,我只使用了一个布尔值和触发器。
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding pass}" Value="true">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding pass}" Value="false">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
所以在这个例子中(我没有修改太多),我根据传递值(它只是一个布尔值)改变背景颜色。
DataGrid Foreground="White" FontSize="13" x:Name="datagrid_results" AutoGenerateColumns="False" ItemsSource="{Binding ItemsClass, UpdateSourceTrigger=PropertyChanged}" Margin="3,5,1.6,35" MaxHeight="260"> <DataGrid.Columns> <DataGridTextColumn IsReadOnly="True" Width="*" Header="Minimum" Binding="{Binding Minimum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <DataGridTextColumn IsReadOnly="True" Width="*" Header="Maximum" Binding="{Binding Maximum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <DataGridTextColumn IsReadOnly="True" Width="100" Header="Pass-Fail" Binding="{Binding Pass_Fail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="{Binding Pass_Fail , Converter={StaticResource s2b}}" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid>
从 xaml
的数据网格第一行中删除 Mode=TwoWaypublic partial class MainWindow : Window { List<ItemsClass> items = new List<ItemsClass>(); public MainWindow() { InitializeComponent(); this.DataContext = this; items.Add(new ItemsClass() { Pass_Fail="Pass" }); items.Add(new ItemsClass() { Pass_Fail = "Fail" }); items.Add(new ItemsClass() { Pass_Fail = "Pass" }); items.Add(new ItemsClass() { Pass_Fail = "Fail" }); datagrid.ItemsSource = items; } }