更新单个单元格的 UWP DataGrid 样式
Update a UWP DataGrid style for single cell
我有一个 UWP DataGrid,想在 C# 中为在 CellEditEnded 事件中编辑的选定行和单元格设置单元格样式。到目前为止,我只能为 DataGrid 中的每一行设置列样式。
Style style = new Style();
style.TargetType = typeof(DataGridCell);
style.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Red)));
e.Column.CellStyle = style;
您使用的代码将单元格样式应用于整个列。目前无法直接获取单元格容器,因此无法直接更改单元格容器。
您的方案的解决方法是我们能够获取特定单元格中的内容。例如,DataGrid 的默认内容是 TextBlock。然后你可以改变它的前景。
private void dataGrid1_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
{
//Style style = new Style();
//style.TargetType = typeof(DataGridCell);
//style.Setters.Add(new Setter(DataGridCell.ForegroundProperty, new SolidColorBrush(Windows.UI.Colors.Red)));
//e.Column.CellStyle = style;
TextBlock d = e.Column.GetCellContent(e.Row) as TextBlock;
d.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
}
我有一个 UWP DataGrid,想在 C# 中为在 CellEditEnded 事件中编辑的选定行和单元格设置单元格样式。到目前为止,我只能为 DataGrid 中的每一行设置列样式。
Style style = new Style();
style.TargetType = typeof(DataGridCell);
style.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Red)));
e.Column.CellStyle = style;
您使用的代码将单元格样式应用于整个列。目前无法直接获取单元格容器,因此无法直接更改单元格容器。
您的方案的解决方法是我们能够获取特定单元格中的内容。例如,DataGrid 的默认内容是 TextBlock。然后你可以改变它的前景。
private void dataGrid1_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
{
//Style style = new Style();
//style.TargetType = typeof(DataGridCell);
//style.Setters.Add(new Setter(DataGridCell.ForegroundProperty, new SolidColorBrush(Windows.UI.Colors.Red)));
//e.Column.CellStyle = style;
TextBlock d = e.Column.GetCellContent(e.Row) as TextBlock;
d.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
}