wpf TemplateBinding 如何在 UserControl 模板中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14974202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:37:55  来源:igfitidea点击:

How is TemplateBinding working in UserControl Template?

wpfxamluser-controlscontroltemplatetemplatebinding

提问by winterTTr

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

我是创建 UserControl 的新手,现在我正在尝试自定义 UserControl 模板,如下所示:

<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:

同时我在后端代码中创建了dependencyproperty:

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 TemplateBindingin XAML to bind Fillproperty of my PieButton to fill the path object. The Visual Studio Designer warns me that "the Fill property is not accessible or recognized".

我想TemplateBinding在 XAML 中使用Fill我的 PieButton 的属性来填充路径对象。Visual Studio 设计器警告我“无法访问或识别 Fill 属性”。

Based on my understanding, the TemplateBindingfind the property name from the element that apply this ControlTemplate, which should be PieControlhere, but why the FillProperty cannot access here?

根据我的理解,TemplateBinding从应用这个 ControlTemplate 的元素中找到属性名称,应该在PieControl这里,但是为什么FillProperty 不能在这里访问?

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.

但是我还是觉得TemplateBinding在这个场景下应该可以工作,所以请在这里指出我的错误。谢谢。

回答by Klaus78

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

根据TemplateBinding to DependencyProperty on a custom control is not working,TemplateBinding 不适用于控件上的自定义依赖属性。

As a solution it is suggested to use

作为解决方案,建议使用

{Binding MyProperty, RelativeSource={RelativeSource TemplatedParent}}

{Binding MyProperty, RelativeSource={RelativeSource TemplatedParent}}