I'm trying to get the IsMouseOver trigger working for datatemplate. For some reason it is not firing. I also tried the http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html but I do not see anything in the trace. Here is the code:
XAML:
<Window x:Class="FirstSImpleDataApp.Window4" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window4" Height="300" Width="300"> <Window.Resources> <ResourceDictionary> <DataTemplate x:Key="tmptemplate"> <Border x:Name="brd" BorderBrush="Black" BorderThickness="2"> <TextBlock x:Name="txt">my text box</TextBlock> </Border> <DataTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="txt" Property="Background" Value="Red"></Setter> <Setter TargetName="txt" Property="Foreground" Value="Green"></Setter> <Setter TargetName="brd" Property="Background" Value="Green"></Setter> </Trigger> </DataTemplate.Triggers> </DataTemplate> </ResourceDictionary> </Window.Resources> <Canvas x:Name="can" Loaded="can_Loaded"> </Canvas> </Window> Code behind:
public partial class Window4 : Window { public Window4() { InitializeComponent(); } private void can_Loaded(object sender, RoutedEventArgs e) { var tmp = this.TryFindResource("tmptemplate") as DataTemplate; var obj = (FrameworkElement)tmp.LoadContent(); can.Children.Add(obj); } } Any help is appreciated!