I am new to creating UserControl, and now I am trying to customize the UserControl template as shown 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 am creating a dependency property in the internal 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 the Fill property of my PieButton to populate the path object. The designer of Visual Studio warns me that "the Fill property is not available or not recognized."
Based on my understanding, TemplateBinding find the property name from the element that uses this ControlTemplate, which should be a PieControl here, but why does the Fill property not have access here?
By the way
I check the following binding and it may work for me
Color="Binding Fill,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}"
But I still think that TemplateBinding should work in accordance with this scenario, so please indicate my error here. Thanks.
winterTTr
source share