【问题标题】:WPF Programmatically add DataGridTextColumn with DataTriggerWPF 以编程方式使用 DataTrigger 添加 DataGridTextColumn
【发布时间】:2016-06-26 23:12:25
【问题描述】:

我有一个由用户动态组成的DataGrid。这意味着每次运行时,列可能并且将会非常不同。出于这个原因,每一列都是以编程方式添加的。我需要在其中添加一些DataTriggers,所以认为这会起作用:

Style style = new Style();
style.TargetType = typeof(DataGridTextColumn);
DataTrigger tg = new DataTrigger()
{
    Binding = new Binding(value),
    Value = "bad data"
};
tg.Setters.Add(new Setter()
{
    Property = UIElement.VisibilityProperty,
    Value = Visibility.Hidden
});

虽然这在 IDE 中运行时不会出现错误,但它会崩溃并给我 'DataGridTextColumn' type must derive from FrameworkElement or FrameworkContentElement.

以编程方式将DataTrigger 添加到DataGridTextColumn 的正确方法是什么

【问题讨论】:

    标签: c# wpf datagrid datatrigger programmatically-created


    【解决方案1】:

    您需要使用typeof(DataGridCell)。触发器应应用于Cell 本身,而不是Column

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2010-11-21
      • 2016-01-05
      相关资源
      最近更新 更多