AttachedProperties追加プロパティ

2567 ワード

付加属性も依存属性ですが、具体的には例を見てください
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());

        }