簡體   English   中英

WPF:使自定義控件中的模板中的嵌入式控件的屬性可用

[英]WPF: Making Properties of embedded control from template in custom control available

我有一個自定義控制IconMD與屬性IconNameOverlayNameOverlayPosition

我已將此控件嵌入另一個自定義控件IconButton如下所示:

<ControlTemplate TargetType="{x:Type local:IconButton}">
    <Border 
        Background="{TemplateBinding Background}"
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}">
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Setter Property="Opacity" Value="0.7"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Opacity" Value="1"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <Grid>
            <local:IconMD
                x:Name="_ButtonIcon"
                OverlayPosition="{TemplateBinding OverlayPosition}"
                IconName="{TemplateBinding IconName}"
                OverlayName="{TemplateBinding OverlayIconName}"
            />
        </Grid>
    </Border>
</ControlTemplate>

IconButton控件的Dependency Properties OverlayPositionIconNameOverlayIconName的唯一目的只是傳遞給嵌入式IconButtonBorderBrush等相同。

現在,由於我還具有IconToggleButtonIconRepeatButton (它們分別繼IconButton類,而不能繼承自IconButton ),因此我必須為它們每個重復此模式。 如果我的IconMD類的功能增強,我將不得不在使用它的每個自定義控件中擴展此模式。

我怎么可以簡單地命名(的屬性) IconMD控制“_ButtonIcon”可我之外IconButton

我會想像一下

<imCC:IconButton
    IconName="mdi-account-card-details"
    OverlayIconName="mdi-multiplication-box"/>

寫這樣的東西

<imCC:IconButton
    _ButtonIcon.IconName="mdi-account-card-details"
    _ButtonIcon.OverlayName="mdi-multiplication-box"/>

您可以將屬性定義為可以在任何UIElementButton元素上設置的附加屬性。

附加屬性概述: https : //docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/attached-properties-overview

namespace WpfApplication1
{
    public static class AttachedProperties
    {
        public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
            "IconName", typeof(string), typeof(AttachedProperties));

        public static void SetIconName(UIElement element, string value)
        {
            element.SetValue(IsBubbleSourceProperty, value);
        }
        public static string GetIconName(UIElement element)
        {
            return (string)element.GetValue(IsBubbleSourceProperty);
        }
    }
}

<imCC:IconButton xmlns:local="clr-namespace:WpfApplication1"
    local:AttachedProperties.IconName="mdi-account-card-details" />

您可以使用附加屬性代替普通的依賴項屬性(在Visual Studio中使用propa創建)。

如果創建IconName作為類附加屬性IconButton ,您進行如下設置:

<imCC:IconButton
    imCC:IconButton.IconName="mdi-account-card-details"
    ...

並在ControlTemplate像這樣使用:

<local:IconMD
    x:Name="_ButtonIcon"
    IconName="{Binding Path=(local:IconButton.IconName),RelativeSource={RelativeSource TemplatedParent}}"
    ...

暫無
暫無

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

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