Use of additional attributes

Additional properties

1, Why additional attributes need to be defined:

  • An additional attribute is a dependent attribute. Different from the dependent attribute, the class to which the additional attribute is applied is not the class that defines the additional attribute.

  • A dependency attribute does not belong to an object, but the object needs the dependency attribute due to some requirements. In this case, the dependency attribute needs to be defined as an additional attribute.

2, A shortcut to create additional attributes: enter propa and press Tab twice

3, Do not use attribute wrappers: because additional attributes can be used for any object.

  • Wrap SetValue() and GetValue() by calling two static methods, which should be named SetPropertyName() and GetPropertyName().
1 public static bool GetIsRotate(DependencyObject obj)
2 {
3     return (bool)obj.GetValue(IsRotateProperty);
4 }
5  
6 public static void SetIsRotate(DependencyObject obj, bool value)
7 {
8      obj.SetValue(IsRotateProperty, value);
9 }

4, Definition of additional attributes:

(1) The additional attribute templates generated with shortcut keys are as follows: the RigisterAttached method needs to be used.

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

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

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

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

1 public static readonly DependencyProperty IsRotateProperty =
2             DependencyProperty.RegisterAttached("IsRotate", typeof(bool), typeof(YMDemo), new PropertyMetadata(0));
  • Parameter 1 (IsRotate): the name of the additional attribute to be registered.

  • Parameter 2 (typeof(bool)): declare that the type of attribute IsRotate is bool.

  • Parameter 3 (typeof(YMDemo)): the owner type of the dependent attribute IsRotate, that is, the class that defines the additional attribute is YMDemo.

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

1 public PropertyMetadata(object defaultValue);
2 
3 public PropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback, CoerceValueCallback coerceValueCallback);
  •   DefaultValue: the default value is used when the additional property is not explicitly assigned.

  •   PropertyChangedCallback: this delegate will be called after the value of the additional property is changed. This delegate can be associated with an influence function.

  •   CoerceValueCallback: when the value of the additional attribute 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 attribute. 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) Define the additional attribute IsRotate in YMDemo.xaml.cs;

(2) In MainWindow.xaml, if you want the Grid control to use IsRotate property:

<Window x:Class="DependencyPropertyDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DependencyPropertyDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
<Grid local:YMDemo.IsRotate="True"/> </Window>

Summary: provide the use entry of this property for any required control (class).

Keywords: Front-end WPF

Added by Fizzgig on Sun, 05 Dec 2021 02:49:03 +0200