0
votes

I have an Item class which has 2 properties, "Value" and "IsSelected". The "Value" format is something like "Column1=Value1::Column2=Value2::Column3=Value3", and "IsSelected" is a boolean value.

I have a list of this Item class, which I need to bind to a datagrid. So, basically I need to split the "Value" string and construct a DataTable to bind it to the datagrid. Then, if the Item's "IsSelected" is true, I will need to color the row with a color.

The problem I'm having is, I do not know how can I present my data in a datagrid while checking the "IsSelected" property to set the row color. How can I bind my data such that each datarow is binding to each Item class?

Thanks!

1
Are you familiar with binding? - Bruno Joaquim
Hi Bruno, yes I am. I know that I can bind the list to the DataGrid's ItemSource, but then I can't access the "IsSelected" property for each of the row then. - Dhinnesh Jeevan
Is your Datagrid creating the columns automatically? Or you define manually? - Bruno Joaquim
The DataGrid columns are automatically created. The number of columns are undefined. I think I missed explaining a part. If the "Value" is "Column1=Value1::Column2=Value2::Column3=Value3", it means that I need to have 3 columns. So the number of columns depends on the "Value". It can have 3 columns, 4 columns, or more. - Dhinnesh Jeevan

1 Answers

0
votes

You could style your DataGridRow, here's an example:

 <DataGrid ItemsSource="{Binding Things}">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Background" Value="Red"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <Setter Property="Background" Value="Green"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

I just did a silly example, however, you just have to write your on style in order to bind your property IsSelected in which row, in my case, I create a Datatrigger which is based on the value of the property IsSelected, just like your case, and whenever the value of this property is true, I change the background of my row to green.

And that is it, it works like expected. enter image description here