0

I have the following class:

public class Order
{
    public int Id { get; set; }
    public string OrdName { get; set; }
    public int Quant { get; set; }
    public int Supplied { get; set; }
}

and this DataGrid:

<DataGrid x:Name="dgOrders" Margin="5" CanUserAddRows="False" CanUserDeleteRows="False"
              SelectionMode="Extended" ItemsSource="{Binding}"
              SelectionUnit="FullRow" VerticalScrollBarVisibility="Auto"
              Height="300" Width="700" HorizontalAlignment="Left" AutoGenerateColumns="False" Grid.Row="2">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Order Name" Binding="{Binding OrdName}" IsReadOnly="True"/>
            <DataGridTextColumn Header="Quantity" Binding="{Binding Quant}" IsReadOnly="True"/>
            <DataGridTextColumn Header="Supplied" Binding="{Binding Supplied}" IsReadOnly="True"/>
        </DataGrid.Columns>
</DataGrid>

What I want is that when the Quantity and Supplied properties are equal the row background color will change.
I tried it with an Event Trigger and a Converter but with no luck (possibly I didn't implement them in the xaml correctly). also trying to do this from the code behind didn't work (tried to get the row instance like this suggests but I keep getting null for the row).

Community
  • 1
  • 1
Yoav
  • 3,326
  • 3
  • 32
  • 73

1 Answers1

1

A Binding cannot be set on the Value property of a DataTrigger. Therefore, you'll need to add an extra property into your data type class:

public bool AreQuantityAndSuppliedEqual
{
    return Quantity == Supplied;
}

Then, with this property, you can use the following Style:

<Style x:Key="EqualRowStyle" TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding AreQuantityAndSuppliedEqual}" Value="True">
            <Setter Property="Background" Value="LightGreen" />
        </DataTrigger>
    </Style.Triggers>
</Style>

It would be used like so:

<DataGrid ItemsSource="{Binding YourItems}" 
    RowStyle="{StaticResource EqualRowStyle}" ... />
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Thanks Sheridan. just one small correction, i think that the Binding should be to the new property name and not to `IsSelected`. – Yoav Nov 28 '13 at 14:51
  • Yes sorry, that property was from the test class that I was using. I've updated it now... thanks for pointing that out. – Sheridan Nov 28 '13 at 14:57