Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF Datagrid更改单击按钮所在行的背景色_C#_Wpf_Datagrid_Background - Fatal编程技术网

C# 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

我的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>

您可以在每行中设置颜色属性,将其绑定到行背景颜色,并在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
必须是
画笔