How does the Dependency Property (Dependencyproperty) "work"?

Asked

Viewed 247 times

1

I’m trying to understand the feature of dependency properties. I’ve read some tutorials on msdn but the feature is still unclear to 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 don’t understand the "mechanics" of the operation of class methods UserControl1. That is, at what time they are being called, and by whom.

1 answer

4


So that a property of a Usercontrol(Dependencyobject) can be used in XAML and thus 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 recorded in "WPF Property system".

This is done using the static method Register() class Dependencyproperty.

One Dependency Property is composed of two "parts":

  • Property identifier - An instance of Dependencyproperty obtained by the return of the method DependencyProperty.Register() and kept as a static member of the Dependencyobject. This identifier is used as a parameter for many of the Apis that interact with the "WPF Property system".
    The convention requires that your name be the property plus Property

    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 GetValue() and SetValue() of Dependencyobject to implement the property’s "get" and "set".

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

The method DependencyProperty.Register() has 3 overloads:

When registering the property, you can also enter its owner’s name, type and type callbacks,

Those callbacks sane:

So, in answer to your question, these methods are being called by "WPF Property system" while interacting with property.

More information on Dependency Properties Overview.

  • Does the property necessarily need to be static? I don’t understand the role of the static method on loaded OnSetTextChanged I see that it just creates an object and calls the method that will make the change.

  • 1

    Yes the property needs to be static. Regarding the method OnSetTextChanged(), was something I also noticed, it does not need to be overloaded. It is only necessary to declare it with the signature void OnSetTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e), which is the one that corresponds to delegate Propertychangedcallback, and there put the code to execute when the property is changed, in this case, tbTest.Text = e.NewValue.ToString();.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.