C# 如何为datagrid wpf的单个单元格而不是单行设置背景?
我有这个代码,我想改变datagrid单个单元格的背景 在我的代码中,更改行的背景C# 如何为datagrid wpf的单个单元格而不是单行设置背景?,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有这个代码,我想改变datagrid单个单元格的背景 在我的代码中,更改行的背景 <DataGrid x:Name="dg"> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Style.Triggers> <DataTrigger Binding="{Binding
<DataGrid x:Name="dg">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Place}"
Value="true">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
我搜索了DataGridView的相关信息和代码:
this.dataGridView1.Rows[0].Cells[1].Style.ForeColor = System.Drawing.Color.Red;
但这对wpf的DataGrid不起作用
如何更改DataGrid wpf的此代码?DataTrigger绑定到属性Place并检查值“true”。我还没有使用数据表,但似乎您试图绑定到字符串类型的列“Place”。数据网格期望Place为bool或具有值“true”(字符串)。毕竟,如果您更改了绑定属性的值,您必须通知属性()的更改。DataTrigger绑定到property Place并检查值“true”。我还没有使用数据表,但似乎您试图绑定到字符串类型的列“Place”。数据网格期望Place为bool或具有值“true”(字符串)。毕竟,如果您更改了绑定属性的值,则必须通知属性()的更改。您可以通过编程方式设置单元格背景颜色
DataGridRow dgRow = mydatagrid.ItemContainerGenerator.ContainerFromIndex(mydatagrid.SelectedIndex) as DataGridRow;
DataGridCell cell = mydatagrid.Columns(2).GetCellContent(dgRow).Parent as DataGridCell;
cell.Background = New SolidColorBrush(Colors.Blue);
可以通过编程方式设置单元格背景色
DataGridRow dgRow = mydatagrid.ItemContainerGenerator.ContainerFromIndex(mydatagrid.SelectedIndex) as DataGridRow;
DataGridCell cell = mydatagrid.Columns(2).GetCellContent(dgRow).Parent as DataGridCell;
cell.Background = New SolidColorBrush(Colors.Blue);
这个问题的答案是用C#编写以下代码:
这个问题的答案是用C#编写以下代码:
谢谢你的回答。但它缺少以下代码:dataGrid.UpdateLayout();谢谢你的回答。但它缺少以下代码:dataGrid.UpdateLayout();
dataGrid.UpdateLayout();
DataGridRow dgRow = mydatagrid.ItemContainerGenerator.ContainerFromIndex(mydatagrid.SelectedIndex) as DataGridRow;
DataGridCell cell = mydatagrid.Columns(2).GetCellContent(dgRow).Parent as DataGridCell;
cell.Background = New SolidColorBrush(Colors.Blue);