Use of dependent properties

Posted by The Cat on Wed, 01 Dec 2021 09:47:03 +0100

Dependent properties

1, Why do I need to define dependent properties

  • User defined attributes are generally common attributes, which are often used as Binding sources; If you want a user-defined attribute as the Binding target, you need to define it as a dependent attribute.
  • For ready-made controls provided by WPF, the reason why all properties in the control can be used as Binding targets is that all ready-made controls of WPF implement dependent properties.

2, To create a shortcut to a dependent attribute: enter propdp and press Tab twice

3, Add attribute wrapper: wrap GetValue and SetValue methods with CLR attributes.

1 public bool IsRotate
2 {
3   get { return (bool)GetValue(IsRotateProperty); }
4   set { SetValue(IsRotateProperty, value); }
5 }
  • When creating a property wrapper, it should only include calls to SetValue() and GetValue() methods, and should not add any validation properties, function codes that cause events, etc. This is because the additional function code will cause the property wrapper to be ignored and the SetValue() and GetValue() methods to be called directly. To verify the correct location of data or raise events, use the dependent property callback function.

4, Definition of dependent properties:

(1) The dependency attribute templates generated by shortcut keys are as follows:  

1 public static readonly DependencyProperty MyPropertyProperty =
2             DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

(2) There are many ways to register dependent properties, and the above is the most commonly used one. There are up to five registration parameters:

1 public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback);

(3) Edit your dependent attribute IsRotate based on the template:

1 public static readonly DependencyProperty IsRotateProperty =
2             DependencyProperty.Register("IsRotate", typeof(bool), typeof(XVision2DRotate), new PropertyMetadata(false));
  • Parameter 1 (IsRotate): the name of the dependent attribute to be registered.
  • Parameter 2 (typeof(bool)): declare that the type of attribute IsRotate is bool.
  • Parameter 3 (typeof (xvision2dlotte)): the owner type of the dependent attribute IsRotate, that is, the class is xvision2dlotte.
  • The type of parameter 4 is the PropertyMetadata class: this class has 5 overloaded constructors, and the maximum number of parameters is three. In the example, the constructor with the parameter DefaultValue is used.
public PropertyMetadata(object defaultValue);

public PropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback, CoerceValueCallback coerceValueCallback);
  •   DefaultValue: the default value is used when the dependent property is not explicitly assigned.
  •   PropertyChangedCallback: this delegate will be called after the value of the dependent property is changed. This delegate can be associated with an influence function.
  •   CoerceValueCallback: when the dependent property value is forcibly changed, this delegate will be called to check the assignment and forcibly assign the value. The return value is of Object type. This is the value to be assigned to the property. This delegate can be associated with an influence function.

5, After the attribute value is changed, the correction process is as follows:

  6, Use of dependent properties

(1) IsRotate can be used as the Binding source in xvision2dlotte.xaml:

Visibility="{Binding IsRotate,Converter={StaticResource B2VConverter}}"

(2) When xvision2dlotte.xaml is embedded into MainWindow.xaml as UserControl, IsRotate can be used as the Binding target, which is reflected in the role of dependent attributes:

<local:XVision2DRotate IsRotate="{Binding path=Text,Converter={StaticResource T2BConverter}}">

Effect: in this way, we can freely assign IsRotate in MainWindow.xaml. Here, a common attribute Text is bound. After the IsRotate Value changes, a series of Value correction in xvision2dlotate will be triggered, and the Object after correction will be assigned to Visibility.