using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace ConsoleApplication1
{
    public class DPCategory : DependencyObject
    {
        public static readonly DependencyProperty NameProperty =
             DependencyProperty.Register("Name", typeof(string), typeof(DPCategory), new PropertyMetadata(string.Empty));

        //public void TestOverrideMetadata()
        //{
        //    //DependencyProperty提供了OverrideMetadata方法重写属性元数据,但一个依赖属性在同一个类中不允许重写,即在同一类型中,保证属性元数据在依赖属性中的唯一性
        //    NameProperty.OverrideMetadata(typeof(DPCategory), new PropertyMetadata("WPF"));//错误示例代码
        //}



        public string Group
        {
            get { return (string)GetValue(GroupProperty); }
            set { SetValue(GroupProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Group.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GroupProperty =
            DependencyProperty.Register("Group", typeof(string), typeof(DPCategory), new PropertyMetadata(string.Empty, new PropertyChangedCallback((d, e) => {
                Console.WriteLine(e.NewValue);
            })));



        public int Order
        {
            get { return (int)GetValue(OrderProperty); }
            set { SetValue(OrderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Order.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrderProperty =
            DependencyProperty.Register("Order", typeof(int), typeof(DPCategory), new UIPropertyMetadata(0));



    }

    /// <summary>
    /// 重写属性元数据,继承的子类允许重写属性元数据
    /// 1.重写元数据是一个以重写元数据为主的合并的过程,即子类的重写后的元数据会与父类的进行合并
    /// 2.实例匹配(Type.IsAssignableFrom 返回为true),注意:要重写的父类属性元数据类型必须要是重写的属性元数据的父类或者与之类型相同.可以看出如果用FrameworkPropertyMetadata来重写的话是最保险的
    /// </summary>
    public class DPWPFCategory : DPCategory
    {
        public void TestOverrideMetadata()
        {
            NameProperty.OverrideMetadata(typeof(DPWPFCategory), new PropertyMetadata("WPF"));
            Console.WriteLine(NameProperty.GetMetadata(typeof(DPWPFCategory)).DefaultValue);

            GroupProperty.OverrideMetadata(typeof(DPWPFCategory), new PropertyMetadata("Computer Technology"));
            Console.WriteLine(this.Group);
            this.Group = "Technology";

            //OrderProperty.OverrideMetadata(typeof(DPWPFCategory), new UIPropertyMetadata(1001));
            OrderProperty.OverrideMetadata(typeof(DPWPFCategory), new FrameworkPropertyMetadata(1002));

            Console.WriteLine(this.Order);
        }
    }

    /// <summary>
    /// 改写属性元数据
    /// 改写是将已存在的依赖属性添加到不同类型当中
    /// 改写不是一个合并属性元数据的过程,改写后的是全新的
    /// </summary>
    public class DPGroup:DependencyObject
    {

        public string Group
        {
            get { return (string)GetValue(GroupProperty); }
            set { SetValue(GroupProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Group.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GroupProperty = DPCategory.GroupProperty.AddOwner(typeof(DPGroup), new PropertyMetadata("Technology"));

        public void TestAddOwnerMetadata()
        {
            Console.WriteLine(Group);
            this.Group = "Computer Technology";
            Console.WriteLine(Group);
        }

    }

}
站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: