1

following the post in here, I've managed to create Tag property to the columns of my datagrid. Now, I would like to have different colors of columns depending on the value in tag. The goal is to have a table with days in columns, and today's current day would be highlighted (the date corresponding to column is stored in it's tag). Is it achievable? I found the solution based on coloring cells programmatically, but it doesn't suit my needs (multithreading issue - sometimes cell is painted before it stops to fill with data and then painting doesn't work).

Thanks in advance.

Edit:

Ok, I've followed the answer given by Nayeem Mansoori and came to this:

Style:

<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Background" Value="{TemplateBinding local:DataGridCellThemeProps.Brush}" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#B2DCDCDC" />
                <Setter Property="BorderBrush" Value="#B2DCDCDC" />
            </Trigger>
            <Trigger Property="IsSelected" Value="False">
                <Setter Property="Background" Value="#7FFFFFFF" />
                <Setter Property="BorderBrush" Value="#7FFFFFFF" />
            </Trigger>
        </Style.Triggers>
    </Style>

DataGrid column:

<DataGridTextColumn local:DataGridCellThemeProps.Brush="Thistle" 
                                Header="{x:Static props:Resources.Monday}" 
                                Binding="{Binding Monday, Converter={StaticResource ZeroConverter}, Mode=TwoWay}" 
                                CellStyle="{StaticResource CellStyle}" Width="75"/>

Class:

public static class DataGridCellThemeProps {
    public static SolidColorBrush GetBrush(DependencyObject obj) {
        return (SolidColorBrush)obj.GetValue(BrushProperty);
    }

    public static void SetBrush(DependencyObject obj, SolidColorBrush value) {
        obj.SetValue(BrushProperty, value);
    }

    public static readonly DependencyProperty BrushProperty =
        DependencyProperty.RegisterAttached(
            "Brush",
            typeof(SolidColorBrush),
            typeof(DataGridCellThemeProps),
            new FrameworkPropertyMetadata(Brushes.Black));
}

When I run my app, I get exception:

Exception: {"'Set property 'System.Windows.Setter.Value' threw an exception.' Line number '25' and line position '14'."}

Inner Exception: {"Expression type is not a valid Style value."}

I've double checked namespace of the class, it's correct. Any ideas?

Community
  • 1
  • 1
greenskin
  • 544
  • 8
  • 22
  • 1
    Have you try this link : http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/ I think it will help you – Nayeem Mansoori Dec 30 '13 at 12:48
  • You should use DataTriggers for this. See http://msdn.microsoft.com/en-us/library/system.windows.datatrigger(v=vs.110).aspx – Patrick Hofman Dec 30 '13 at 12:45

0 Answers0