c# - WPF 以编程方式使用 DataTrigger 添加 DataGridTextColumn

标签 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”类型必须派生自 FrameworkElement 或 FrameworkContentElement。

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

最佳答案

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

关于c# - WPF 以编程方式使用 DataTrigger 添加 DataGridTextColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944772/

相关文章:

c# - 在 Window Loaded WPF C# 中跳过代码

.net - WPF - 将列表框绑定(bind)到列表 <string> - 我做错了什么?

wpf - 在样式中设置 DataGrid.GroupStyle

c# - 带有异步/等待的 System.Threading.Timer 卡在重复中

c# 对自定义列表进行排序

json - 如何将动态 json 绑定(bind)到 treeview wpf

c# - 使用转换器设置 WPF 单元格样式

c# - 如何在 WPF 中为 DataGrid 设置一个 float 的删除行按钮?

c# - 如何在 MVC Razor 的 HTML.TextAreaFor 文本中显示链接?

c# - 为什么我仍然可以序列化没有属性 [System.SerializableAttribute()] 的类?