0

I'm trying to set a specific color for each cell in my DataGrid that have a specific value. I saw a lot on the net, but I doesn't found one that approach my situation. Essentially I've this DataGrid structure:

<DataGrid ItemsSource="{Binding MatchService.Matches}" AutoGenerateColumns="False" 
                      CanUserAddRows="false" IsReadOnly="True">
    <DataGrid.Columns>
        <DataGridTextColumn Header="{DynamicResource championship}" Binding="{Binding Competition}"/>
        <DataGridTextColumn Header="1" Binding="{Binding HomeWin}"/>
        <DataGridTextColumn Header="X" Binding="{Binding Draw}"/>
    </DataGrid.Columns>

so I need to use a pure xaml solution, in particular a generic style that handle all the cell value. For example, if HomeWin cell have the value <50 the cell background will be red also if the cell value is >60 the background will be green.

How can I create a default style and bind it for each DataGridTextColumn without wrote converter or such?

Thanks.

JDOE
  • 19
  • You can define a defult style for `DataGridTextColumn`, but if you want property value comparisons in triggers other than "equals", you'll have to write a converter. And don't write a NumberToBrush converter. Write a numeric comparison converter and set the brush in a DataTrigger which uses that trigger. – 15ee8f99-57ff-4f92-890c-b56153 Oct 24 '16 at 13:54
  • 1
    There is no pure XAML solution. You'll have to implement and use a Binding Converter. This is the fourth time that I close this question as a duplicate. Please stop asking the same question again and again. A second StackOverflow user account also isn't very welcome here. – Clemens Oct 24 '16 at 13:55
  • @Clemens, strictly speaking, there is a pure xaml solution: dataTrigger in cellStyle for each possible value. however it is applicable only for finite (and better small) sets of values (e.g, enums, bool) – ASh Oct 24 '16 at 14:03
  • @ASh I've only double value. – JDOE Oct 24 '16 at 14:05
  • 1
    @Clemens a moderate who gives the ignorant and even downvote to a user. Congratulations, expect a post complaint in the coming days. – JDOE Oct 24 '16 at 14:30

1 Answers1

1

1.Create a NumberToBrush converter like this:

[ValueConversion(typeof(int), typeof(Brush))]
public class NumberToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int val = (int)value;
        if (val < 50)
            return Brushes.Red;
        if (val > 60)
            return Brushes.Green;

        return Brushes.Transparent;
    }

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

2.Add it to your View resources:

<Window.Resources>
    <wpfApplication1:NumberToBrushConverter x:Key="NumberToBrushConverter"/>
</Window.Resources>

3.Use DataGridTemplateColumn instead of DataGridTextColumn and redefine your DataTemplate to use the converter with your binded value:

   <DataGrid.Columns>
            <DataGridTemplateColumn Header="X">
                <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate DataType="wpfApplication1:YourModel">
                            <TextBox Text="{Binding X}" Background="{Binding Path=X, Converter={StaticResource NumberToBrushConverter}}" />
                        </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
       </DataGrid.Columns>
Rom
  • 1,183
  • 1
  • 8
  • 18
  • thanks for the answer, just for curiosity: is possible create a case condition like a swith something like : `switch(val){case if(val<50){return Brushes.Red;}}` – JDOE Oct 24 '16 at 14:08
  • of course, just replace your if conditions with switch case – Rom Oct 24 '16 at 14:11
  • could you please show me how? Thanks. – JDOE Oct 24 '16 at 14:14
  • I tried your solution and seems working, but is possible fill the cell instead of the textblock? – JDOE Oct 24 '16 at 14:20
  • This is how you can check range with switch case: http://stackoverflow.com/a/31662661/3955716 – Rom Oct 24 '16 at 16:46