0

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?

1 Answer 1

1

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">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.