簡體   English   中英

WPF 使用 DataTrigger 以編程方式添加 DataGridTextColumn

[英]WPF Programmatically add DataGridTextColumn with DataTrigger

我有一個由用戶動態組成的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的正確方法是什么

您需要使用typeof(DataGridCell) 觸發器應該應用於Cell本身,而不是Column

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM