WPF以编程方式将DataGridTextColumn与DataTrigger一起添加

时间:2016-03-11 16:04:01

标签: c# wpf datagrid datatrigger programmatically-created

我有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以编程方式

的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

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