C# WPF Datagrid更改单击按钮所在行的背景色
我的wpf应用程序中有一个数据网格。我希望当用户单击datagrid中的任何按钮时,相应的行应该是红色的。我想我们可以通过使用事件触发器来实现,但我真的不知道如何使用它C# WPF Datagrid更改单击按钮所在行的背景色,c#,wpf,datagrid,background,C#,Wpf,Datagrid,Background,我的wpf应用程序中有一个数据网格。我希望当用户单击datagrid中的任何按钮时,相应的行应该是红色的。我想我们可以通过使用事件触发器来实现,但我真的不知道如何使用它 <DataGrid x:Name="dgEmp" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" > <DataGrid.Columns> <DataGridT
<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>
您可以在每行中设置颜色属性,将其绑定到行背景颜色,并在SelectedItem更改时进行更改
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding RowColour}" />
</Style>
</DataGrid.RowStyle>
您可以在每行中设置颜色属性,将其绑定到行背景颜色,并在SelectedItem更改时进行更改
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding RowColour}" />
</Style>
</DataGrid.RowStyle>
对我不起作用。无论是
还是
在这些情况下,我都不会将行的背景设置为窗体的颜色。@Astrogator您是否收到任何绑定错误?您是绑定到集合中的颜色还是祖先或集合中的颜色?这确实有效。诀窍是:如果RowColor
的类型为Color
,则必须将其包装在
元素中,如回答中所示。否则RowColor
必须是画笔
。对我不起作用。无论是
还是
在这些情况下,我都不会将行的背景设置为窗体的颜色。@Astrogator您是否收到任何绑定错误?您是绑定到集合中的颜色还是祖先或集合中的颜色?这确实有效。诀窍是:如果RowColor
的类型为Color
,则必须将其包装在
元素中,如回答中所示。否则,RowColor
必须是画笔
。