Anya
Anya

Reputation: 105

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
}

Upvotes: 1

Views: 1954

Answers (2)

Vladislav Zorov
Vladislav Zorov

Reputation: 3056

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

Upvotes: 1

JSJ
JSJ

Reputation: 5691

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>

Upvotes: 2

Related Questions