Jrow
Jrow

Reputation: 1054

Using binding in DataTrigger

I'm new to WPF and trying to figure out how to change the background color of a datagrid row based on the value of a column. I've seen a few examples of people using datatriggers:

<DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding result}" Value="1">
                            <Setter Property="Background" Value="Red"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding result}" Value="0">
                            <Setter Property="Background" Value="Green"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
</DataGrid.RowStyle>

I can't figure out how the code side of this works.
I bind the DataTable to the DataGrid like this:

episodeDataGrid.DataContext = episodeTable.DefaultView;

Can I trigger the DataTrigger based on the text of one of the columns? For example two rows:

Amount | result

4000 | 0

5000 | 1

The 4000 row having the result value 0 will be green and the 5000 row having the result value 1 will be red?

Upvotes: 0

Views: 542

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

The DataTable.DefaultView is actually different than your data table.

Either, set the data context to the table itself :

episodeDataGrid.DataContext = episodeTable;

Or, adjust your binding :

<DataTrigger Binding="{Binding Row.result}" Value="1">

Upvotes: 1

Related Questions