How is a class that inherits DependencyObject used?
c#, wpf
Solution
The long and short of it is you likely won't need DependencyObject's if you go Model-View-ViewModel, but to answer your specific questions:
DependencyProperty definitions are static because all instances of the DependencyObject share the definition. They are like a fancier per-object property Dictionary. This also allows for various framework features to work seamlessly such as Binding or rendering updates.
DependencyProperty Coercion occurs when you need to chain together changes of properties. For example, if you had a Slider control which has a Value that should exist between a Minimum and Maximum, you use the CoerceValue callback to ensure it remains within the proper range.
DependencyObjects have fallen out of favor in user code, and instead are largely supplanted by the Model-View-ViewModel pattern in WPF development. Where you'll still find DependencyObjects are in custom control development. DependencyProperties are common in user code, usually in the form of Attached Properties.
Problem
I was following a tutorial on the Dependency Object from here: http://tech.pro/tutorial/745/wpf-tutorial-introduction-to-dependency-properties Yet, I'm still slightly confused. I created the following class which is purely for my own learning purposes and has no real usage: ``` 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 don't fully understand if it's meant to store a value at the object level. - What does the Coerce callback do and why is it included? - What is the purpose of my class and where would I use it?