繁体   English   中英

如何为模板控件创建自定义属性

[英]How to create custom properties to a Template Control

我有一个模板控件,它的根是StackPanel。 我想要的是一种可以做这样的事情:

<Controls:MyControl Kind="SomeKind"/>

并且基于SomeKind,StackPanel的背景将发生变化。 例如,将存在有限数量的“种类”,以相同的方式,在Button中存在有限数量的Horizo​​ntalAlignments。

<Button HorizontalAlignment="Center"/>

我已经在互联网上进行了一些搜索,似乎确实涉及到Attached Properties,但是我还没有找到UWP的干净,简单且易于遵循的示例。 我找到了一些WPF的示例,但它们似乎不起作用。

不,您不需要附加属性,因为它可能仅在自定义控件中关联。 您需要的是enum类型的依赖项属性

假设您有这个enum -

public enum PanelBackgroundType
{
    Orange,
    Pink,
    Offwhite
}

然后,您的依赖项属性将如下所示:

public PanelBackgroundType PanelBackgroundType
{
    get { return (PanelBackgroundType)GetValue(PanelBackgroundTypeProperty); }
    set { SetValue(PanelBackgroundTypeProperty, value); }
}

// Using a DependencyProperty as the backing store for PanelBackgroundType.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty PanelBackgroundTypeProperty =
    DependencyProperty.Register("PanelBackgroundType", typeof(PanelBackgroundType), typeof(MyControl), 
    new PropertyMetadata(PanelBackgroundType.Offwhite, (s, e) =>
    {
        if ((PanelBackgroundType)e.NewValue != (PanelBackgroundType)e.OldValue)
        {
            // value really changed, invoke your changed logic here
            var control = (MyControl)s;

            switch ((PanelBackgroundType)(e.NewValue))
            {
                case PanelBackgroundType.Orange:
                    control.MyStackPanel.Background = new SolidColorBrush(Colors.Orange);
                    break;
                case PanelBackgroundType.Pink:
                    control.MyStackPanel.Background = new SolidColorBrush(Colors.Pink);
                    break;
                case PanelBackgroundType.Offwhite:
                default:
                    control.MyStackPanel.Background = new SolidColorBrush(Colors.Wheat);
                    break;
            }
        }
        else
        {
            // else this was invoked because of boxing, do nothing
        }
    }));

请注意,我在属性已更改的回调中进行了检查(PanelBackgroundType)e.NewValue != (PanelBackgroundType)e.OldValue ,以查看dp的值是否确实已更改。 这似乎是多余的,但是根据MSDN ,这是最佳实践,因为-

如果DependencyProperty的类型是枚举或结构,则即使该结构的内部值或枚举值未更改,也可以调用该回调。 这与系统原语(例如字符串)不同,在该原语中,只有在值更改时才调用它。 这是内部对这些值进行装箱和拆箱操作的副作用。 如果您的值是枚举或结构的属性具有PropertyChangedCallback方法,则需要通过自己强制转换值并使用可立即广播的值使用的重载比较运算符来比较OldValue和NewValue。 或者,如果没有可用的此类运算符(自定义结构可能就是这种情况),则可能需要比较各个值。 如果结果是值未更改,则通常不选择执行任何操作。

看一下此链接: https : //msdn.microsoft.com/zh-cn/windows/uwp/xaml-platform/custom-dependency-properties

它将向您展示如何为可以在UI中编辑的对象添加自定义依赖项属性。

尽管我建议您看一下Microsoft的文档,但我将给您一个有关所需事件的快速示例。

public sealed partial class MyControl : UserControl
{
    public MyControl()
    {
        this.InitializeComponent();
    }

    public static readonly DependencyProperty KindProperty = DependencyProperty.Register(
    "Kind", typeof(string), typeof(MyControl),
    new PropertyMetadata(null, new PropertyChangedCallback(OnKindChanged)));

    public string Kind
    {
        get { return (string)GetValue(KindProperty); }
        set { SetValue(KindProperty, value); }
    }

    private static void OnKindChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Executed method when the KindProperty is changed
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM