Killnine
Killnine

Reputation: 5900

Style WPF DataGrid Row on Binding Property

I have a data grid bound to a list of EventRecords. On the EventRecord object is a property IsAutoEvent. I'm trying to style the background color on the row depending on whether this property is true or not but the DataContext is not set up as I expect.

<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsAutoEvent}">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

Currently, this doesn't do anything. I don't think the datacontext is right because, under DataTrigger, the {Binding} shows properties for the View (e.g. MainViewModel), not the DataGridRow (e.g. EventRecord), as I would expect.

Any thoughts?

Upvotes: 0

Views: 3497

Answers (2)

MRebai
MRebai

Reputation: 5474

Please add the Value to your DataTrigger

<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

Upvotes: 1

Nitin Purohit
Nitin Purohit

Reputation: 18578

you have not put the Value on DataTrigger

               <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>

Upvotes: 3

Related Questions