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 6225849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:55:10+00:00 2026-05-24T08:55:10+00:00

Value={TemplateBinding HeaderColor}I’ve created my own control, and I’m wondering if I can bind a

  • 0

Value=”{TemplateBinding HeaderColor}”I’ve created my own control, and I’m wondering if I can bind a Border.Background to a template property. Currently i’m setting it with a StaticResource like the following:

<Color x:Key="ControlMouseOverColor">green</Color>

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
    <EasingColorKeyFrame KeyTime="0:0:6" Value="{StaticResource ControlMouseOverColor}" />
</ColorAnimationUsingKeyFrames>

I’d like it be a property on my control, and be able to set it as a template binding

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
    <EasingColorKeyFrame KeyTime="0:0:6" Value="{TemplateBinding HeaderColor}" />
</ColorAnimationUsingKeyFrames>

MainPage.xaml

<ctrl:Selection Grid.Column="0" HeaderColor="Red" HeaderText="Header Text" />

my class:

public static readonly DependencyProperty HeaderColorProperty =
        DependencyProperty.Register("HeaderColor", typeof(System.Windows.Media.Color), typeof(Selection), new PropertyMetadata(System.Windows.Media.Colors.Red));

public  System.Windows.Media.Color HeaderColor {
    get { return (System.Windows.Media.Color)GetValue(HeaderColorProperty); }
    set { SetValue(HeaderColorProperty, value); }
}

This 2nd option does not work, should I be able to do this? I don’t get an error, it just does not change to the color I set.

Comment left by AngelWPF asked for more code, pasting below, I’m in the beginning stages of learning to create a control, wanted to note that, because there is a lot I have not got to yet, one piece at a time 🙂

generic.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SelectionControl.Library"
xmlns:ctrl="clr-namespace:SelectionControl.Library;assembly=SelectionControl">

<LinearGradientBrush x:Key="HeaderBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="Black" Offset="0" />
    <GradientStop Color="Gray" Offset="1" />
</LinearGradientBrush>

<Color x:Key="ControlMouseEnterColor">aliceblue</Color>
<Color x:Key="ControlMouseLeaveColor">Gray</Color>
<Color x:Key="ControlLeftMouseUpColor">Red</Color>

<Style TargetType="ctrl:Selection">
    <Setter Property="Width" Value="Auto" />
    <Setter Property="Height" Value="Auto" />
    <Setter Property="FontSize" Value="12" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Foreground" Value="AliceBlue" />
    <Setter Property="Margin" Value="2,2,2,2" />

    <Setter Property="Background" Value="{StaticResource ResourceKey=HeaderBackground}" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ctrl:Selection">
                <Grid x:Name="RootElement" Margin="{TemplateBinding Margin}">
                    <!-- Visual States -->
                     <VisualStateManager.VisualStateGroups>
                          <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="MouseEnter">
                              <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
                                        <EasingColorKeyFrame KeyTime="0:0:.5" Value="{TemplateBinding HeaderColor}" />
                                    </ColorAnimationUsingKeyFrames>
                              </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseLeave">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
                                        <EasingColorKeyFrame KeyTime="0:0:1" Value="{StaticResource ControlMouseLeaveColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseLeftUp">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
                                        <EasingColorKeyFrame KeyTime="0:0:1" Value="{StaticResource ControlLeftMouseUpColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    <!-- End Visual States-->

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <!-- Header -->
                    <Border x:Name="headerLayout" Background="{TemplateBinding Background}" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="2,2,2,2" BorderBrush="Black" BorderThickness="1">
                        <StackPanel>
                            <ToggleButton ></ToggleButton>
                            <TextBlock Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding HeaderText}" FontWeight="{TemplateBinding FontWeight}" FontSize="{TemplateBinding FontSize}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </StackPanel>
                    </Border>
                    <!-- Body Content -->
                    <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

Selection.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SelectionControl.Library {
    [TemplateVisualState(Name = Selection.MouseEnterStateName, GroupName = Selection.CommonStatesGroupName)]
    [TemplateVisualState(Name = Selection.MouseLeaveStateName, GroupName = Selection.CommonStatesGroupName)]
    [TemplateVisualState(Name = Selection.MouseLeftUpStateName, GroupName = Selection.CommonStatesGroupName)]

public class Selection : ContentControl {
    public const string CommonStatesGroupName = "CommonStates";
    public const string MouseEnterStateName = "MouseEnter";
    public const string MouseLeaveStateName = "MouseLeave";
    public const string MouseLeftUpStateName = "MouseLeftUp";

    public Selection() {
        this.DefaultStyleKey = typeof(Selection);

        this.MouseEnter += new MouseEventHandler(OnMouseEnter);
        this.MouseLeave += new MouseEventHandler(OnMouseLeave);
        this.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseLeftButtonUp);
    }

    void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
        this.GoToState(Selection.MouseLeftUpStateName, true);
    }

    void OnMouseLeave(object sender, MouseEventArgs e) {
        this.GoToState(Selection.MouseLeaveStateName, true);
    }

    void OnMouseEnter(object sender, MouseEventArgs e) {
        this.GoToState(Selection.MouseEnterStateName, true);
    }
    private void GoToState(string stateName, bool useTransitions) {
        VisualStateManager.GoToState(this, stateName, useTransitions);
    }

    public static readonly DependencyProperty HeaderTextProperty =
        DependencyProperty.Register("HeaderText", typeof(string), typeof(Selection), new PropertyMetadata(""));

    public string HeaderText {
        get { return (string)GetValue(HeaderTextProperty); }
        set { SetValue(HeaderTextProperty, value); }
    }

    public static readonly DependencyProperty HeaderColorProperty =
        DependencyProperty.Register("HeaderColor", typeof(System.Windows.Media.Color), typeof(Selection), new PropertyMetadata(System.Windows.Media.Colors.Red));

    public  System.Windows.Media.Color HeaderColor {
        get { return (System.Windows.Media.Color)GetValue(HeaderColorProperty); }
        set { SetValue(HeaderColorProperty, value); }
    }
}}
  • 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-24T08:55:10+00:00Added an answer on May 24, 2026 at 8:55 am

    I have had mixed results using TemplateBinding on custom dependency properties. Because of this, I have used RelativeSource TemplatedParent which seems to work in every situation.

    <EasingColorKeyFrame KeyTime="0:0:.5" Value="{Binding HeaderColor, RelativeSource={RelativeSource TemplatedParent}}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this work: <Style TargetType=s:Substance> <Setter Property=Template> <Setter.Value> <DataTemplate> <StackPanel> <TextBlock Text={TemplateBinding Name}/>
I'm trying to do something like this... <Style x:Key="MyBorderStyle" TargetType="Border"> <Setter Property="Padding" Value="{TemplateBinding Padding}"
I have created a custom control for ImageButton as <Style TargetType={x:Type Button}> <Setter Property=Template>
I want to bind Border.Background in my Button ControlTemplate to the Background Property of
I have this control template that I am writing: <Style TargetType={x:Type controls:InfoBar}> <Setter Property=Template>
<Style x:Key=FavouriteMenuItemStyle TargetType={x:Type MenuItem} BasedOn={StaticResource BasicFavouriteItemStyle}> <Setter Property=Width Value=Auto/> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType={x:Type
i have button with this style: <Style TargetType=Button x:Key=btnStyle> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=Button>
I have a grid with this template and styles in WPF/XAML: <Setter Property=TextOptions.TextFormattingMode Value=Display
I've tried: <UniformGrid DockPanel.Dock=Top Columns=2 Rows=1> <UniformGrid.Resources> <Style TargetType=RadioButton> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=RadioButton>
I created a silverlight template control. Thouse control consist 4 elements: 2 textbox and

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.