【问题标题】:Update a UWP DataGrid style for single cell更新单个单元格的 UWP DataGrid 样式
【发布时间】:2022-01-15 06:54:14
【问题描述】:

我有一个 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;

【问题讨论】:

    标签: uwp


    【解决方案1】:

    您使用的代码将单元格样式应用于整个列。目前无法直接获取单元格容器,因此无法直接更改单元格容器。

    您的方案的解决方法是我们能够获取特定单元格中的内容。例如,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);
            
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2020-03-27
      • 1970-01-01
      • 2012-08-04
      • 2011-12-25
      相关资源
      最近更新 更多