WPF Datagrid-Change background color of the row fo

2019-06-13 18:13发布

I have a data grid in my wpf application. I wants that when user clicks on any button in the datagrid the corresponding row should be red. I think we can do it by using event trigger but i really don't know how to use it.

    <DataGrid x:Name="dgEmp" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" >          
        <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name}"></DataGridTextColumn>
        <DataGridTextColumn Header="Age" Width="*" Binding="{Binding Path=Age}"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Delete" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete selected row"></Button>                              
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

1条回答
聊天终结者
2楼-- · 2019-06-13 19:09

You can set up a colour property in each row, bind this to the rows background colour and change it as the SelectedItem changes.

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
       <Setter Property="Background" Value="{Binding RowColour}" />
    </Style>
</DataGrid.RowStyle>
查看更多
登录 后发表回答