How is a class that inherits DependencyObject used?

I followed the Dependency object tutorial: http://tech.pro/tutorial/745/wpf-tutorial-introduction-to-dependency-properties

However, I am still a little confused. I created the following class, which is intended solely for my own learning purposes and has no real use:

namespace DPTest
{
    class Audio : DependencyObject
    {
        public static readonly DependencyProperty fileTypeProperty = DependencyProperty.Register("fileType", typeof(String), typeof(Audio),
        new PropertyMetadata("No File Type", fileTypeChangedCallback, fileTypeCoerceCallback), fileTypeValidationCallback);

        public String fileType
        {
            get
            {
                return (String)GetValue(fileTypeProperty);
            }
            set
            {
                SetValue(fileTypeProperty, value);
            }
        }

        private static void fileTypeChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            Console.WriteLine(e.OldValue + " - " + e.NewValue);
        }

        private static object fileTypeCoerceCallback(DependencyObject obj, object o)
        {
            String s = o as String;
            if (s.Length > 0)
            {
                s = s.Substring(0, 8);
            }

            return s;
        }

        private static bool fileTypeValidationCallback(object value)
        {
            return value != null;
        }
    }
}

A few questions:

  • Why is the property static? I do not quite understand if this meant keeping the value at the object level.
  • What does the Coerce callback do and why is it enabled?
  • What is the purpose of my class and where will I use it?
+5
source share
2 answers

, , DependencyObject, Model-View-ViewModel, :

+8

dependency , . , CLR.

() CLR, SetValue GetValue. , . .

coerce - . , 8 . . , . - , , .

- , . , . DependencyObject. , . , bindable XAML .., .

+3

All Articles