Reputation: 141
I am trying to change a column's Foreground color according to its value. When I try to set the Foreground statically it works but when I use binding with a converter nothing happens.
Here is a small example
<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>
This is my Converter:
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;
}
The weird thing is that when debugging this converter, the brush is returning the correct value. It's just the the cell is not changing its text color. However when I used this:<Setter Property="Foreground" Value="Red" />
my cells change their text color. Is there anything that I might be missing?
Upvotes: 0
Views: 1750
Reputation: 381
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>
Remove Mode=TwoWay from your datagrid first line of xaml
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; } }
Upvotes: 3
Reputation: 138
I have been used to make things works even if it is not the way I was doing so if you need this to work fast I can offer you an alternative while the perfect solution is coming. To make thing works I simply used a boolean and the triggers.
<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>
So in this exemple (I didn't modify so much), I change the background color according to the pass value (which is simply a boolean).
Upvotes: 1