繁体   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