Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Datagrid trigger row colour based on value
    primarykey
    data
    text
    <p>I have a WPF application that contains a datagrid. The datagrid is bound to my object OrderBlock which contains a List of type Orders.</p> <pre><code>&lt;DataGrid DataContext="{Binding OrderBlock}" Name="dataGridOrdersGood" ItemsSource="{Binding Orders}" </code></pre> <p>This works fine and displays nicely in my datagrid. There is one property (StatusGood) in my List though that I would like to display as a combobox where there can be only two values, "Send" or "Hold".</p> <p>If the value in the combobox is "Hold" I would like the row to turn different colour. Ideally using a linear gradient going from silver to yellow. I have tried the code below - literally just trying to turn the row red for the moment but nothing happens. I can't see what is wrong with my code below. The trigger part is very close to the bottom of the code below. I'm new to WPF and struggling with it at the moment. The code below has mainly come from a very good post that can be found here, <a href="http://www.codeproject.com/Articles/586132/WPF-DataGrid-Custommization-using-Style-and-Templa" rel="noreferrer">http://www.codeproject.com/Articles/586132/WPF-DataGrid-Custommization-using-Style-and-Templa</a></p> <pre><code> &lt;!-- Data grid formatting Grid Row template --&gt; &lt;Style x:Key="DG_Row" TargetType="{x:Type DataGridRow}"&gt; &lt;Setter Property="Background" Value="LightGreen"/&gt; &lt;Setter Property="Opacity" Value="1"/&gt; &lt;Setter Property="Padding" Value="3,2,2,3"/&gt; &lt;Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type DataGridRow}"&gt; &lt;Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True"&gt; &lt;Border.Background&gt; &lt;LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"&gt; &lt;GradientStop Offset="0" Color="Transparent"/&gt; &lt;GradientStop Offset="1" Color="Silver"/&gt; &lt;/LinearGradientBrush&gt; &lt;/Border.Background&gt; &lt;SelectiveScrollingGrid&gt; &lt;SelectiveScrollingGrid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/SelectiveScrollingGrid.ColumnDefinitions&gt; &lt;SelectiveScrollingGrid.RowDefinitions&gt; &lt;RowDefinition Height="20" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/SelectiveScrollingGrid.RowDefinitions&gt; &lt;DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt; &lt;DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}" /&gt; &lt;DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/&gt; &lt;/SelectiveScrollingGrid&gt; &lt;VisualStateManager.VisualStateGroups&gt; &lt;VisualStateGroup x:Name="CommonStates"&gt; &lt;VisualState x:Name="Normal" /&gt; &lt;VisualState x:Name="Normal_AlternatingRow"&gt; &lt;Storyboard&gt; &lt;ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)"&gt; &lt;EasingColorKeyFrame KeyTime="0" Value="#AAF0C570" /&gt; &lt;/ColorAnimationUsingKeyFrames&gt; &lt;ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)"&gt; &lt;EasingColorKeyFrame KeyTime="0" Value="#AAFF7F00" /&gt; &lt;/ColorAnimationUsingKeyFrames&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;VisualState x:Name="Normal_Selected"&gt; &lt;Storyboard&gt; &lt;!-- ColorAnimation here same as Normal_AlternatingRow state --&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;VisualState x:Name="MouseOver"&gt; &lt;Storyboard&gt; &lt;!-- ColorAnimation here same as Normal_AlternatingRow state --&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; &lt;/VisualStateGroup&gt; &lt;/VisualStateManager.VisualStateGroups&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Active}" Value="Hold"&gt; &lt;Setter Property="Background" Value="Red" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>As always any help would be great. Thanks M</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload