[英]WPF Datagrid-Change background color of the row for which the button is clicked
我的wpf应用程序中有一个数据网格。 我希望当用户单击数据网格中的任何按钮时,对应的行应为红色。 我认为我们可以通过使用事件触发器来做到这一点,但我真的不知道如何使用它。
<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>
您可以在每行中设置一个color属性,将此属性绑定到行的背景色,并在SelectedItem更改时对其进行更改。
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding RowColour}" />
</Style>
</DataGrid.RowStyle>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.