Change Color of Datagrid

Asked

Viewed 89 times

1

People, I need some help, I’ve done some research, but I can’t find it, I would like to know how to change the color of the selected item in Datagrid. Example: When I have a line in the datagrid and I click on it it is selected with the blue color, I would like to change this color. Can someone give me a hint.

1 answer

2

One possibility is to have a property on the object you are listing to do this:

public virtual int BackgroundId { get; set; }

In the click event you can change the value of this variable to differentiate it from the others:

seuObjeto.BackgroundId = 1;

In XAML, within the datagrid a datatrigger is created:

<DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding BackgroundId}" Value="1">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding BackgroundId}" Value="2">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
</DataGrid.CellStyle>

From now on items that have this property changed will also have their background changed. In the example for 1 or 2, but you can set as many as you want in the datatrigger

Browser other questions tagged

You are not signed in. Login or sign up in order to post.