我正在尝试向 WPF 数据模板添加触发器,我将在 DataGrid 中将其用作单元格模板。我需要以编程方式执行此操作,因此它全部在后面的代码中。我希望当您将鼠标悬停在边框上时突出显示边框,但它似乎不起作用。
DataTemplate dt = new DataTemplate();
// other implementation
Trigger t1 = new Trigger() { Property = IsMouseOverProperty, Value = true };
t1.Setters.Add(new Setter(BorderBrushProperty, System.Windows.Media.Brushes.Black));
t1.Setters.Add(new Setter(BorderThicknessProperty, new Thickness(4.0)));
dt.Triggers.Add(t1);
// add to visual tree, and other logic that works perfectly.
唯一不起作用的是这个触发器。当我将鼠标悬停在单元格上时,它不会突出显示边框。怎么了?
您必须指定Trigger
和Setters
正在操作的元素。
设置触发器的SourceName
属性和设置器TargetName
:
var dt = new DataTemplate();
var t1 = new Trigger()
{
SourceName = "source",
Property = IsMouseOverProperty,
Value = true
};
t1.Setters.Add(new Setter(BorderBrushProperty, Brushes.Black, "target"));
t1.Setters.Add(new Setter(BorderThicknessProperty, new Thickness(4.0), "target"));
dt.Triggers.Add(t1);