簡體   English   中英

將附加屬性綁定到控件模板內的附加屬性

[英]Binding attached property to attached property inside a control template

以下是我的問題的一個工作示例: https //github.com/themimcompany/AttachedPropertyIssue

我有一個用於定義ControlTemplateButtonStyle ControlTemplate我有一個具有相同附加屬性的TextBlock 我想將TextBlock的附加屬性設置/綁定到Button的附加屬性的值。 附加屬性是一個簡單的int值。

我該如何工作? 這可能在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" />

再次解釋:我有一個附加屬性分配給Button ,我想將該Button附加屬性的值賦給該Button StyleControlTemplateTextBlock的相同附加屬性。 它不像我期望的那樣工作。 我在運行時得到Unspecified error.. exception。 我該如何工作?

以下是我的問題的一個工作示例: 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>

我想使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM