简体   繁体   中英

wpf color the cells in DataGrid Based on value

I want to change text color of cells based on value that cell contains I use a value converter, but somehow the type of object that is getting passed to Convert function is DataRowView, and i want to pass the Cell, because i want to hightlight one cell at a time based on its value. Hope that makes sense.

Thanks!!

Code if style that i apply to DataGrid:

<UserControl.Resources>
    <local:MyBkColorConverter x:Key="bkColorCvrt"/>
    <Style x:Key="GridStyle" TargetType="DataGrid">
        <Setter Property="ItemsSource" Value="{Binding}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="RowBackground" Value="Transparent" />
        <Setter Property="HeadersVisibility" Value="None" />
        <Setter Property="GridLinesVisibility" Value="None" />
        <Setter Property="SelectionUnit" Value="Cell" />
        <Setter Property="SelectionMode" Value="Single" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="HorizontalScrollBarVisibility" Value="Disabled" />
        <Setter Property="CellStyle">
            <Setter.Value>
                <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="Foreground">
                        <Setter.Value>
                            <Binding Converter="{StaticResource bkColorCvrt}"/>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Black">
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Setter.Value>
        </Setter>
      </Style>
</UserControl.Resources>

And the c# part:

public class MyBkColorConverter: IValueConverter { #region IValueConverter Members

    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //The type of value here is actually DataRowView
        //here i would like to have a cell passed. is that possible to archive?
        return Brushes.LightGray;
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

If you create a style that will apply on each control matches that type.

Simply use the DataGridTemplateColumn and create your custom Template you can do as you wish.

  <DataGridTemplateColumn Header="Name">                       
      <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Foreground="{Binding Name,Converter={StaticResource colconverter}}" Text="{Binding Name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>

It works if you pass a Path to the property that needs to be converted in the Binding .

<DataGridTextColumn Binding="{Binding Path=AgentUtilization, StringFormat=P}" Header="Agent Utilization">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Foreground" Value="{Binding Path=AgentUtilization, Converter={StaticResource UtilizationFormat}}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

The type that gets passed to the converter is then the type of the property in your binding path (of course, after the cast).

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