WPF Datagrid-更改单击按钮的行的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13778944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid-Change background color of the row for which the button is clicked
提问by user1301587
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.
我的 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>
回答by Oli
You can set up a colour property in each row, bind this to the rows background colour and change it as the SelectedItem changes.
您可以在每一行中设置一个颜色属性,将其绑定到行的背景颜色并随着 SelectedItem 的变化而更改它。
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding RowColour}" />
</Style>
</DataGrid.RowStyle>

