WPF Datagrid - 更改单击按钮的行的背景颜色

时间:2012-12-08 15:35:10

标签: c# wpf datagrid background

我的wpf应用程序中有一个数据网格。我希望当用户点击datagrid中的任何按钮时,相应的行应为红色。我想我们可以通过使用事件触发器来实现,但我真的不知道如何使用它。

    <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 个答案:

答案 0 :(得分:4)

您可以在每一行中设置颜色属性,将其绑定到行背景颜色,并在SelectedItem更改时更改它。

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
       <Setter Property="Background" Value="{Binding RowColour}" />
    </Style>
</DataGrid.RowStyle>