如何在不同控件类型中实现依赖属性值继承 [英] How to implement dependency property value inheritance in different control types

查看:25
本文介绍了如何在不同控件类型中实现依赖属性值继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一组自定义控件,每个控件都有各自控件的基类.因为它们每个都有不同的基本控件,所以它们不能共享完全相同的依赖属性.是否可以将依赖属性链接在一起,以便它们可以相互级联?(不确定我是否在语法上正确使用术语级联)

A have a collection of custom controls I'm creating that each have base classes of their respective controls. Because they each have different base controls, they can't share the exact same dependency property. Is it possible to link dependency properties together so that they can cascade from each other? (Not sure if I'm using the term cascade correctly grammar-wise)

public class RCTWindow : ContentControl {
    public static readonly DependencyProperty RemapColorProperty =
        DependencyProperty.RegisterAttached(
        "RemapColor",
        typeof(RemapColors),
        typeof(RCTWindow),
        new FrameworkPropertyMetadata(
            RemapColors.SeaGreen,
            FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            new PropertyChangedCallback(OnVisualChanged)));
    //...
}
public class RCTButton : Button {
    public static readonly DependencyProperty RemapColorProperty =
        DependencyProperty.RegisterAttached(
        "RemapColor",
        typeof(RemapColors),
        typeof(RCTButton),
        new FrameworkPropertyMetadata(
            RemapColors.SeaGreen,
            FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            new PropertyChangedCallback(OnVisualChanged)));
    //...
}

这是xaml.设置父控件的重映射颜色时,按钮默认不层叠.

Here's the xaml. When setting the parent control's remap color, the button does not cascade it by default.

<local:RCTWindow RemapColor="IndianRed">
    <local:RCTButton/>
</local:RCTWindow>

推荐答案

你不应该声明多个独立的附加属性,因为它们之间不会有任何属性值继承.

You should not declare multiple indepedent attached properties, because there won't be any property value inheritance between them.

相反,声明一个附加属性,并在控件类中使用 DependencyProperty.AddOwner.

Instead, declare a single attached property, and use DependencyProperty.AddOwner in the control classes.

public static class RCT
{
    public static readonly DependencyProperty RemapColorProperty =
        DependencyProperty.RegisterAttached(
            "RemapColor", typeof(RemapColors), typeof(RCT),
            new FrameworkPropertyMetadata(RemapColors.SeaGreen,
                FrameworkPropertyMetadataOptions.Inherits |
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public static RemapColors GetRemapColor(DependencyObject obj)
    {
        return (RemapColors)obj.GetValue(RemapColorProperty);
    }

    public static void SetRemapColor(DependencyObject obj, RemapColors value)
    {
        obj.SetValue(RemapColorProperty, value);
    }
}

public class RCTButton : Button
{
    public static readonly DependencyProperty RemapColorProperty =
        RCT.RemapColorProperty.AddOwner(
            typeof(RCTButton), new FrameworkPropertyMetadata(OnVisualChanged));

    public RemapColors RemapColor
    {
        get { return (RemapColors)GetValue(RemapColorProperty); }
        set { SetValue(RemapColorProperty, value); }
    }

    private static void OnVisualChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        Debug.WriteLine("RCTButton.OnVisualChanged: {0}", ((RCTButton)obj).RemapColor);
    }
}

public class RCTWindow : ContentControl
{
    public static readonly DependencyProperty RemapColorProperty =
        RCT.RemapColorProperty.AddOwner(
            typeof(RCTWindow), new FrameworkPropertyMetadata(OnVisualChanged));

    public RemapColors RemapColor
    {
        get { return (RemapColors)GetValue(RemapColorProperty); }
        set { SetValue(RemapColorProperty, value); }
    }

    private static void OnVisualChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        Debug.WriteLine("RCTWindow.OnVisualChanged: {0}", ((RCTWindow)obj).RemapColor);
    }
}

这篇关于如何在不同控件类型中实现依赖属性值继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆