首页 > 解决方案 > 将附加属性绑定到控件模板内的附加属性

问题描述

这是我的问题的一个工作示例: https ://github.com/themimcompany/AttachedPropertyIssue

我有一个定义 a的Stylefor a 。在里面我有一个具有相同附加属性的。我想将 ' 的附加属性设置/绑定到' 的附加属性的值。附加属性是一个简单的值。ButtonControlTemplateControlTemplateTextBlockTextBlockButtonint

我如何让这个工作?这可以在 UWP 中完成吗?我收到的错误没有告诉我如何解决这个问题。像Unspecified error...

这是我的风格:

<Style TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <TextBlock local:Translation.TranslationModelID="{Binding Path=(local:Translation.TranslationModelID),
                                                                                         RelativeSource={RelativeSource TemplatedParent}}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

这是我的附加属性定义:

public class Translation
{
    public static int GetTranslationModelID(DependencyObject obj)
    {
        return (int)obj.GetValue(TranslationModelIDProperty);
    }

    public static void SetTranslationModelID(DependencyObject obj, int value)
    {
        obj.SetValue(TranslationModelIDProperty, value);
    }

    public static readonly DependencyProperty TranslationModelIDProperty =
        DependencyProperty.RegisterAttached("TranslationModelID", typeof(int), typeof(FrameworkElement), new PropertyMetadata(0));

}

这是我要定义的按钮,请注意我要分配按钮的附加属性 - 然后在 ControlTemplate 中获取值并将其分配给 TextBlock 的附加属性(查看样式):

<Button local:Translation.TranslationModelID="1" />

再次解释一下:我有一个附加属性分配给 a Button,我想将 that 的附加属性的值分配给 that的a的Button相同附加属性。它没有按我的预期工作。我在运行时遇到异常。我如何让这个工作?TextBlockControlTemplateButtonStyleUnspecified error..

这是我的问题的一个工作示例: https ://github.com/themimcompany/AttachedPropertyIssue

编辑:

这种方法给了我一个不同的错误。The property 'TranslationModelID' was not found in type 'PassingAttachedPropertyInControlTemplater.Translation'. [Line: 17 Position: 40]

<Page.Resources>
    <Style TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <TextBlock local:Translation.TranslationModelID="{TemplateBinding local:Translation.TranslationModelID}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid>
    <Button x:Name="btn" local:Translation.TranslationModelID="1" />
</Grid>

标签: c#xamluwpuwp-xaml

解决方案


我想使用 TemplateBinding 或常规绑定将模板化父级的附加属性分配给 ControlTemplate 内元素的附加属性

根据您的要求,您可以创建继承按钮的模板化控件。然后在后面的代码中设置 TextBlock Attached 属性。请检查以下段代码。

public sealed class CustomButton : Button, IDisposable
{
    private long token;
    private TextBlock Tbk;

    public CustomButton()
    {
        this.DefaultStyleKey = typeof(CustomButton);
    }

    private void CallBackMethod(DependencyObject sender, DependencyProperty dp)
    {
        UpdateAttachedPropertyValue();
    }

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        token = this.RegisterPropertyChangedCallback(AttachedPropertyTest.Translation.TranslationModelIDProperty, CallBackMethod);
        Tbk = GetTemplateChild("TBK") as TextBlock;
        UpdateAttachedPropertyValue();        
    }

    public void Dispose()
    {
        this.UnregisterPropertyChangedCallback(AttachedPropertyTest.Translation.TranslationModelIDProperty,token);
    }

    private void UpdateAttachedPropertyValue()
    {
        AttachedPropertyTest.Translation.SetTranslationModelID(Tbk, AttachedPropertyTest.Translation.GetTranslationModelID(this));
    }

}

Xaml

<Style TargetType="local:CustomButton" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CustomButton">
                <Border
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <TextBlock x:Name="TBK"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

推荐阅读