Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:40:52+00:00 2026-05-22T11:40:52+00:00

EDIT: Original title: When I add a custom control based on a timer, my

  • 0

EDIT: Original title: When I add a custom control based on a timer, my templates are ignored.

I’m working on the 70-511 training kit from Microsoft Press, and combined two practice exercises together from chapter 5.

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.

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.

As you might infer from my attached code, the custom control is in a separate assembly referenced by the 5_3 project.

I’m a bit stumped on this one. What is causing this?

Here is the code:

MainWindow.xaml:

<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">
<Window.Resources>
    <ControlTemplate TargetType="{x:Type Button}" x:Key="ButtonTemplate">

        <Border Name="Bord1" BorderBrush="Olive" BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
                <Rectangle Name="rect1">
                    <Rectangle.Fill>
                        <SolidColorBrush x:Name="rosyBrush" Color="RosyBrown"/>
                    </Rectangle.Fill>
                </Rectangle>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True" >
                <Trigger.EnterActions>
                    <BeginStoryboard Name="bst2">
                        <Storyboard AutoReverse="False">
                            <ColorAnimation Duration="0:0:.3"
                                            Storyboard.TargetProperty="Color" 
                                            Storyboard.TargetName="rosyBrush" >
                                <ColorAnimation.By>
                                    <Color A="0" R="100" B="0" G="0"/>
                                </ColorAnimation.By>
                            </ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>

                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="bst2" />
                </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard Name="bst1">
                        <Storyboard>
                            <ThicknessAnimation Storyboard.TargetName="Bord1"
                 Storyboard.TargetProperty="BorderThickness"
                 By=".1" Duration="0:0:.3" />
                            <ColorAnimation AutoReverse="False" To="DarkRed" Duration="0:0:.3"
                                            Storyboard.TargetProperty="Color" 
                                            Storyboard.TargetName="rosyBrush" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="bst1" />
                </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="rect1" Property="Fill" Value="Gray"></Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Button Template="{StaticResource ResourceKey=ButtonTemplate}" Height="23" Width="100" BorderThickness="2" Name="btnHello" Content="Hello" IsEnabled="False">

    </Button>
    <ToolBarPanel>
        <CheckBox IsChecked="True" Content="Enable Button" Name="cbEnabled" Checked="cbEnabled_Checked" Unchecked="cbEnabled_Checked"/>

    </ToolBarPanel>
    <my:CustomControl1 Name="customControl11" />
</Grid>

CustomControl1.xaml: (separate assembly)

<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">
<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBlock Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center"
                                   Text="{Binding Path=Time}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

CustomControl.cs

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);

    }
}

Edit:

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: http://snoopwpf.codeplex.com/ It has saved me a lot of time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:40:52+00:00Added an answer on May 22, 2026 at 11:40 am

    Because your Button and CustomControl are in the same row and column of the Grid, your CustomControl is probably covering the Button. You probably just can’t see it.

    If you set the Background of your CustomControl to say Red, then you will see what area it is covering.

    You would need to ensure that the CustomControl doesn’t cover the Button, if you want the Button to respond to mouse events. Alternatively, you can set IsHitTestVisible to false on your CustomControl or ensure it’s Background is null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Edit: From another question I provided an answer that has links to a lot
EDIT!! I should add that this site runs perfectly locally. It's only when deployed
Edit: original question below, but I revise it now that I have some code
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT What small things which are too easy to overlook do I need to
Edit : Solved, there was a trigger with a loop on the table (read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.