Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a class that inherits DependencyObject used?

Tags:

c#

wpf

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:

  1. Why is the property static? I don't fully understand if it's meant to store a value at the object level.
  2. What does the Coerce callback do and why is it included?
  3. What is the purpose of my class and where would I use it?
like image 568
James Jeffery Avatar asked Mar 14 '13 00:03

James Jeffery


People also ask

What is a DependencyObject?

The DependencyObject class enables Windows Presentation Foundation (WPF) property system services on its many derived classes. The property system's primary function is to compute the values of properties, and to provide system notification about values that have changed.

What is a Dependencyproperty in c#?

A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. Not only this, but a Dependency Property also has the built-in feature of providing notification when the property has changed, data binding and styling.

What is dependency property?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.


2 Answers

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:

  1. 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.

  2. 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.

  3. 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.

like image 171
user7116 Avatar answered Sep 21 '22 23:09

user7116


The dependency property itself is static, because you are assigning the property to the object type. It identifies the property as a whole, and does not just capture the value as CLR properties do.

As you can see in the (non-static) CLR property, the storage functionality is handled by SetValue and GetValue alone. You can imagine the dependency property values as a big dictionary where you just put some arbitrary content into. The key for the dictionary is the dependency property.

The coerce callback is used to force the value into some format. In your case it will make sure that the string is only 8 characters long. Most times, you don’t need such a callback. You don’t even need a property changed callback that often. The most common case is just bindable properties and there it’s enough to just define the dependency property with standard metadata.

The purpose of your class is to serve as an example I guess. If you don’t have a specific use case, I wouldn’t advise you to even look into dependency objects. You actually very rarely subtype DependencyObject itself. Usually you have a framework element or component which you inherit and add custom behaviour to it. And then, whenever you need a bindable property for XAML etc., a dependency property is needed.

like image 22
poke Avatar answered Sep 20 '22 23:09

poke