简体   繁体   中英

WPF DataGrid cell foreground binding not working

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?

With static color: 静态颜色

With dynamic color: 动态色彩

在此处输入图像描述 >

 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; } }

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM