Reputation: 6586
I am trying to add triggers to a WPF DataTemplate, which I will use as a cell template in my DataGrid. I need to do this programatically, so it is all in the code behind. I want the border to be highlighted when you mouse over it, but it doesn't seem to work.
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.
The only thing that doesn't work is this trigger. It doesn't highlight the border when I mouse over the cells. What's wrong?
Upvotes: 1
Views: 3458
Reputation: 128061
You have to specify the elements that the Trigger
and the Setters
are operating on.
Set the SourceName
property of the Trigger and the TargetName
of the Setters:
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);
Upvotes: 3