Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is TemplateBinding working in UserControl Template?

I am new one to create UserControl and now I am trying to customize the UserControl Template as below:

<UserControl x:Class="WpfApplication1.PieButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Loaded="OnLoaded">
    <UserControl.Template>
        <ControlTemplate>
            <Path Name="path" Stroke="Aqua" StrokeThickness="3">
                <Path.Fill>
                    <SolidColorBrush Color="{TemplateBinding Fill}" />
                </Path.Fill>
                <Path.Data>
                ......
</UserControl>

At the same time I have create the dependencyproperty in back-end code:

public partial class PieButton : UserControl
{
    public PieButton()
    {
        InitializeComponent();
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {

    }



    public Color Fill
    {
        get { return (Color)GetValue(FillProperty); }
        set { SetValue(FillProperty, value); }
    }

    public static readonly DependencyProperty FillProperty =
        DependencyProperty.Register("Fill", typeof(Color), typeof(PieButton));
    ......

I want to use TemplateBinding in XAML to bind Fill property of my PieButton to fill the path object. The Visual Studio Designer warns me that "the Fill property is not accessible or recognized".

Based on my understanding, the TemplateBinding find the property name from the element that apply this ControlTemplate, which should be PieControl here, but why the Fill Property cannot access here?

BTW,

I test the following binding, and it can work for me

Color="Binding Fill,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}"

But I still think the TemplateBinding should be able to work under this scenario, so please point out my fault here. Thanks.

like image 432
winterTTr Avatar asked Feb 20 '13 07:02

winterTTr


People also ask

What is TemplateBinding?

A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay} . A TemplateBinding is always a one-way binding, even if properties involved default to two-way binding.

How do you access elements that are embedded inside the ControlTemplate?

When you declare elements inside the ControlTemplate, use the 'Name' of each control for identifying it from the outside. We can then use the 'FindName' function for finding the resource. An example shown below is for accessing the TextBlock of a button.

What is a control template?

The ControlTemplate allows you to specify the visual structure of a control. The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.

When creating custom templated controls what interface do you implement?

To create a templated user control In the user control's code, implement a property of type ITemplate. Define a server control class that implements the INamingContainer interface as a container in which to create an instance of the template. This is called the template's naming container.


1 Answers

According to TemplateBinding to DependencyProperty on a custom control is not working, TemplateBinding does not work for custom dependency properties on controls.

As a solution it is suggested to use

{Binding MyProperty, RelativeSource={RelativeSource TemplatedParent}}

like image 197
Klaus78 Avatar answered Oct 12 '22 19:10

Klaus78