AttachedProperties 附加属性

附加属性也是依赖属性,具体请看例子

class Student:DependencyObject
    {

        public static int GetGrade(DependencyObject obj)
        {
            return (int)obj.GetValue(GradeProperty);
        }

        public static void SetGrade(DependencyObject obj, int value)
        {
            obj.SetValue(GradeProperty, value);
        }

        // Using a DependencyProperty as the backing store for Grade.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GradeProperty =
            DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(Student), new UIPropertyMetadata(0));

    }
class Human:DependencyObject
    {

    }
private void Button_Click(object sender, RoutedEventArgs e)
        {
            Student stu = new Student();
            Human h = new Human();
            Student.SetGrade(h, 6);
            MessageBox.Show(Student.GetGrade(h).ToString());
        }
原文地址:https://www.cnblogs.com/HelloMyWorld/p/2921355.html