该行的WPF Datagrid的,更改背景颜色点击为其按钮 [英] WPF Datagrid-Change background color of the row for which the button is clicked

查看:861
本文介绍了该行的WPF Datagrid的,更改背景颜色点击为其按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的WPF应用程序数据网格。我想,当用户点击任意按钮DataGrid中的相应行应为红色。我认为,我们可以通过使用事件触发做到这一点,但我真的不知道如何使用它。

 < D​​ataGrid的X:名称=dgEmpGrid.Row =1的AutoGenerateColumns =FALSECanUserAddRows =FALSE>
        < D​​ataGrid.Columns>
        < D​​ataGridTextColumn标题=名称WIDTH =*绑定={绑定路径=名称}>< / DataGridTextColumn>
        < D​​ataGridTextColumn标题=时代WIDTH =*绑定={绑定路径=年龄}>< / DataGridTextColumn>
            &所述; DataGridTemplateColumn部首=删除宽度=*>
                < D​​ataGridTemplateColumn.CellTemplate>
                    <&DataTemplate的GT;
                        <按钮内容=删除选定行>< /按钮>
                    < / DataTemplate中>
                < /DataGridTemplateColumn.CellTemplate>
            < / DataGridTemplateColumn>
        < /DataGrid.Columns>
    < / DataGrid的>


解决方案

您可以设置颜色属性的每一行中,结合这行的背景色和改变它的的SelectedItem的变化。

 < D​​ataGrid.RowStyle>
    <风格的TargetType =DataGridRow>
       < setter属性=背景VALUE ={结合RowColour}/>
    < /样式和GT;
< /DataGrid.RowStyle>

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.

    <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.

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

这篇关于该行的WPF Datagrid的,更改背景颜色点击为其按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆