Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat can cause my template to be ignored in this Custom Control
    primarykey
    data
    text
    <p><strong>EDIT:</strong> Original title: When I add a custom control based on a timer, my templates are ignored.</p> <p>I'm working on the 70-511 training kit from Microsoft Press, and combined two practice exercises together from chapter 5. </p> <p>The problem is that when I add the custom control to my MainWindow, it runs, but the triggers on the Button template are ignored. When the same control is removed, the triggers are honored. </p> <p>For those who don't have access to the book, and don't feel like analyzing the code, it's a custom control with a label that has a dependency property setup to update on a timer object (once per second) with the current system time.</p> <p>As you might infer from my attached code, the custom control is in a separate assembly referenced by the 5_3 project. </p> <p>I'm a bit stumped on this one. What is causing this? </p> <p>Here is the code:</p> <p>MainWindow.xaml:</p> <pre><code>&lt;Window x:Class="chapter5_3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:chapter5_3CustomControl;assembly=chapter5_4CustomControl"&gt; &lt;Window.Resources&gt; &lt;ControlTemplate TargetType="{x:Type Button}" x:Key="ButtonTemplate"&gt; &lt;Border Name="Bord1" BorderBrush="Olive" BorderThickness="{TemplateBinding BorderThickness}"&gt; &lt;Grid&gt; &lt;Rectangle Name="rect1"&gt; &lt;Rectangle.Fill&gt; &lt;SolidColorBrush x:Name="rosyBrush" Color="RosyBrown"/&gt; &lt;/Rectangle.Fill&gt; &lt;/Rectangle&gt; &lt;ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True" &gt; &lt;Trigger.EnterActions&gt; &lt;BeginStoryboard Name="bst2"&gt; &lt;Storyboard AutoReverse="False"&gt; &lt;ColorAnimation Duration="0:0:.3" Storyboard.TargetProperty="Color" Storyboard.TargetName="rosyBrush" &gt; &lt;ColorAnimation.By&gt; &lt;Color A="0" R="100" B="0" G="0"/&gt; &lt;/ColorAnimation.By&gt; &lt;/ColorAnimation&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/Trigger.EnterActions&gt; &lt;Trigger.ExitActions&gt; &lt;StopStoryboard BeginStoryboardName="bst2" /&gt; &lt;/Trigger.ExitActions&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsPressed" Value="True"&gt; &lt;Trigger.EnterActions&gt; &lt;BeginStoryboard Name="bst1"&gt; &lt;Storyboard&gt; &lt;ThicknessAnimation Storyboard.TargetName="Bord1" Storyboard.TargetProperty="BorderThickness" By=".1" Duration="0:0:.3" /&gt; &lt;ColorAnimation AutoReverse="False" To="DarkRed" Duration="0:0:.3" Storyboard.TargetProperty="Color" Storyboard.TargetName="rosyBrush" /&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/Trigger.EnterActions&gt; &lt;Trigger.ExitActions&gt; &lt;StopStoryboard BeginStoryboardName="bst1" /&gt; &lt;/Trigger.ExitActions&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter TargetName="rect1" Property="Fill" Value="Gray"&gt;&lt;/Setter&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Button Template="{StaticResource ResourceKey=ButtonTemplate}" Height="23" Width="100" BorderThickness="2" Name="btnHello" Content="Hello" IsEnabled="False"&gt; &lt;/Button&gt; &lt;ToolBarPanel&gt; &lt;CheckBox IsChecked="True" Content="Enable Button" Name="cbEnabled" Checked="cbEnabled_Checked" Unchecked="cbEnabled_Checked"/&gt; &lt;/ToolBarPanel&gt; &lt;my:CustomControl1 Name="customControl11" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>CustomControl1.xaml: (separate assembly)</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:chapter5_3CustomControl"&gt; &lt;Style TargetType="{x:Type local:CustomControl1}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:CustomControl1}"&gt; &lt;Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"&gt; &lt;TextBlock Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" Text="{Binding Path=Time}" /&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p></p> <p>CustomControl.cs</p> <pre><code>public class CustomControl1 : Control { public static readonly DependencyProperty TimeProperty; System.Timers.Timer myTimer = new System.Timers.Timer(); delegate void SetterDelegate(); static CustomControl1() { FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(); TimeProperty = DependencyProperty.Register("Time", typeof(string), typeof(CustomControl1), metadata); DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1))); } public CustomControl1() { myTimer.Elapsed += timer_elapsed; myTimer.Interval = 1000; myTimer.Start(); this.DataContext = this; } void TimeSetter() { SetValue(TimeProperty, DateTime.Now.ToLongTimeString()); } void timer_elapsed(object sender, System.Timers.ElapsedEventArgs e) { Dispatcher.Invoke(new SetterDelegate(TimeSetter), System.Windows.Threading.DispatcherPriority.Normal); } } </code></pre> <p><strong>Edit:</strong></p> <p>I wanted to plug a free tool that I use called snoop! You can find it here, and I recommend it as it allows you to inspect your controls at runtime! Snoop Lives here at time of edit: <a href="http://snoopwpf.codeplex.com/" rel="nofollow">http://snoopwpf.codeplex.com/</a> It has saved me a lot of time!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload