How does "DependencyProperty" work?

1

I'm trying to understand the dependency properties feature. I have read some tutorials in msdn but the feature is still obscure for me.

Example:

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3 {

   /// <summary> 
      /// Interaction logic for UserControl1.xaml 
   /// </summary> 

   public partial class UserControl1 : UserControl {

      public UserControl1() {
         InitializeComponent();
      }

      public static readonly DependencyProperty
         SetTextProperty = DependencyProperty.Register("SetText", typeof(string), 
         typeof(UserControl1), new PropertyMetadata("", new PropertyChangedCallback(OnSetTextChanged)));

      public string SetText {
         get {return(string) GetValue(SetTextProperty); }
         set {SetValue(SetTextProperty, value);}
      }

      private static void OnSetTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
         UserControl1 UserControl1Control = d as UserControl1;
         UserControl1Control.OnSetTextChanged(e);
      }

      private void OnSetTextChanged(DependencyPropertyChangedEventArgs e) {
         tbTest.Text = e.NewValue.ToString();
      }
   }
}       

XAML

<Window x:Class = "WpfApplication3.MainWindow"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:views = "clr-namespace:WpfApplication3" 
   Title = "MainWindow" Height = "350" Width = "604">

   <Grid>
      <views:UserControl1 SetText = "Hellow World" />
   </Grid>

</Window>

I'm not understanding the "mechanics" of how methods work for the UserControl1 class. That is, at what time they are being called, and by whom.

    
asked by anonymous 03.01.2017 / 17:46

1 answer

4

For a property of a UserControl ( DependencyObject ) can be used in XAML and thus can be accessed / calculated by means other than the traditional "get" and "set", such as themes , binding , animations, templates, etc., it must be declared as DependencyProperty and registered in the " WPF property system ".

This is done using the static% method of the DependencyProperty class .

A Dependency Property is made up of two "parts":

  • Property identifier - An instance of DependencyProperty obtained by returning the Register() method and saved as a static member of DependencyObject . This identifier is used as a parameter for many of the APIs that interact with the "WPF property system" . The agreement requires that your name be the property plus DependencyProperty.Register()

    public static readonly DependencyProperty SetTextProperty = 
        DependencyProperty.Register("SetText", typeof(string), typeof(UserControl1),
                 new PropertyMetadata("", new PropertyChangedCallback(OnSetTextChanged)));
    
  • CLR "wrapper" - The actual implementation of the property. This implementation uses the identifier and methods Property and GetValue() of DependencyObject to implement the "get" and "set" of the property.

    public string SetText 
    {
         get {return(string) GetValue(SetTextProperty); }
         set {SetValue(SetTextProperty, value);}
    }
    

The SetValue() method has 3 overloads :

When registering the property, in addition to indicating the name, type and type of its owner it is possible to indicate several callbacks ,

These callbacks are:

So, by answering your question, these methods are being called by the "WPF property system" while interacting with the property.

More information on Dependency Properties Overview . >     

03.01.2017 / 20:05