【问题标题】:WPF datagrid - custom colors for each cellWPF datagrid - 每个单元格的自定义颜色
【发布时间】:2013-11-26 01:21:02
【问题描述】:

在我的应用程序中,我以编程方式为 DataGrid 构建所有行和列。我现在想以编程方式为每个单元格控制不同的 UI 方面,例如颜色、字体、字体大小、边框等。我想知道是否有任何单元格绘制事件可以添加回调并在每个单元格的基础上设置所有方面。一个例子将不胜感激。

提前感谢您的帮助。

【问题讨论】:

  • 您了解 WPF 模板系统吗?我会考虑使用模板来实现这一点 - 我很确定它将涵盖数据网格中的单元格级别样式(虽然我可能错了!)

标签: wpf user-interface datagrid wpfdatagrid


【解决方案1】:

我找到的最佳解决方案是捕获 DataGrid 的 OnLoadingRow()。以下链接显示了一个工作示例:

How to set background of a datagrid cell during AutoGeneratingColumn event depending on its value?

【讨论】:

    【解决方案2】:

    我认为没有。我和你有同样的问题,我的解决方案是创建我自己的自定义列并覆盖 GenerateElement。我会告诉你我能做到的代码:

    <cdgc:ColorTextColumn Binding="{Binding value}"
    

    然后:

    protected override System.Windows.FrameworkElement GenerateElement(System.Windows.Controls.DataGridCell cell, object dataItem)
        {
            var text = new TextBlock();
            text.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            text.SetBinding(TextBlock.TextProperty, this.CopyBinding(this.DataBinding, null, null, null));
    
            text.Foreground = (System.Windows.Media.Brush).....;
    
            return text;
        }
    

    如您所见,我之前设置了绑定并在单元格生成中维护

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 2011-06-27
      • 2013-12-27
      • 2019-07-06
      相关资源
      最近更新 更多