Search code examples
wpfxamldatatrigger

WPF - XAML - Data Trigger without Binding / Value only trigger


I have a grid of values in a DataGrid. In each cell I have one of 4 values, I want each value to have an associated colour.

In the past all my data triggers have been of the format;

<DataTrigger Binding="{Binding Status}" Value="R">
     <Setter Property="xcdg:DataRow.Background" Value="Pink" />
</DataTrigger>

But I need something of the format;

<DataTrigger Binding="{Binding *}" Value="R">
     <Setter Property="xcdg:DataRow.Background" Value="Pink" />
</DataTrigger>

So that any cell's value will be checked. The reason I can't use explicit bindings is that the number of columns and their names is dynamic, I use extend CustomTypeDescriptor to expose the columns and rows to enter the grid.

Thanks in advance!


Solution

  • <DataTrigger Binding="{Binding Path=Content, RelativeSource={x:Static RelativeSource.Self}}" Value="Failure">
        <Setter Property="xcdg:DataRow.Background" Value="Red" />
    </DataTrigger>
    

    I needed to use a Relative source.

    Further to that what I ended up using was;

            <xcdg:DataGridControl.Resources>
                <c:ColorConverter x:Key="colorConverter" />
                <Style TargetType="{x:Type xcdg:DataCell}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}}" Value="False">
                            <Setter Property="xcdg:DataCell.Background" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource colorConverter}}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsDirty, RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}}" Value="True">
                            <Setter Property="xcdg:DataCell.Background" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource colorConverter}}" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </xcdg:DataGridControl.Resources>
    

    Where my Colour converter looks a bit like this;

    [ValueConversion(typeof(DataCell), typeof(Brush))]
    public class ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var cell = (DataCell)value;
            var content = (string)cell.Content;
    
            if (content == null || cell.ParentRow.IsSelected)
            {
                return DependencyProperty.UnsetValue;
            }
    
            if (content == "Unknown")
            {
                return new SolidColorBrush(Colors.LightYellow);
            }
    
            ...
    
            return DependencyProperty.UnsetValue;
        }
    
        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    

    My only problem now is that when I scroll my background colours aren't updated when the DataRows are reused.

    I can't find an event to hook into when a DataRow is reused either...