问题
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:
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>
来源:https://stackoverflow.com/questions/13778944/wpf-datagrid-change-background-color-of-the-row-for-which-the-button-is-clicked