简体   繁体   English

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

[英]WPF Datagrid-Change background color of the row for which the button is clicked

I have a data grid in my wpf application. 我的wpf应用程序中有一个数据网格。 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>

You can set up a colour property in each row, bind this to the rows background colour and change it as the SelectedItem changes. 您可以在每行中设置一个color属性,将此属性绑定到行的背景色,并在SelectedItem更改时对其进行更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM