当前位置:网站首页>WPF dependent properties

WPF dependent properties

2022-06-21 22:34:00 zLulus

Concept

A dependent attribute is one that has no value , adopt Binding Get value from data source , It's relying on other people , Objects with dependent properties are called dependent objects

Scenarios where dependent attributes are applied

1、 Set properties in the style .
2、 Property supports data binding .
3、 Use dynamic resource references to set properties .
4、 Automatically inherit attribute values from the parent element in the element tree .
5、 Attributes can be animated .
6、 The attribute system is in the attribute system 、 Reports when an environment or user performs an action or reads and changes the previous value of a property using a style .
7、 Use the established 、WPF Processes also use metadata conventions ,
For example, report whether the layout system is required to rewrite the visual object of an element when changing attribute values . Dependent objects are created without a storage data space .WPF You must use the dependent object as the host of the dependent property in .

For data binding

Dependent attributes are static attributes for a class
Each class instance goes to this static attribute Get The attribute that belongs to you

Objects have properties , Property to the public data area 【 The dependent properties of all class instances are placed in a static variable ( Public area ) Inside 】 Take inside
advantage : The public data area is not UI Threads , So you can change the value at any time

Tips

WPF Interface elements are dependent objects , All attributes are dependent , So binding is supported
 Insert picture description here

Example

Custom control

public class CustomBorder : Border
{
    public CustomBorder()
    {
        // A background color is given by default during initialization  
        Background = Brushes.Blue;
    }
    

    // attribute 
    public Double Transparency
    {
        get { return (Double)GetValue(TransparencyDependency); }
        set { SetValue(TransparencyDependency, value); }
    }

    // Register dependency properties 
    public readonly static DependencyProperty TransparencyDependency =
        DependencyProperty.Register( 
        // Property name 
        "Transparency",
        // Property data type 
        typeof(Double),
        // The owner 
        typeof(CustomBorder),
        // processing method 
        new PropertyMetadata(new PropertyChangedCallback(transparencyPropertyChangedCallback))
        );

    // Processing when the attribute is modified 
    static void transparencyPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        CustomBorder border = (sender as CustomBorder);
        if (border != null)
        {
            border.Opacity = 1 - Convert.ToDouble(e.NewValue) / 255;
        }
    }
}

DependencyProperty Of Register Method

There are several overloading methods , Here are two
 Insert picture description here

//
//  Abstract :
//      Use the specified attribute name 、 Attribute types 、 Owner type and attribute metadata register dependent attributes .
//
//  Parameters :
//   name:
//      The name of the dependent property to register .
//
//   propertyType:
//      Type of attribute .
//
//   ownerType:
//      Registering owner type of dependent property .
//
//   typeMetadata:
//      Attribute metadata that depends on attributes .
//
//  Return results :
//      A dependent attribute identifier , You should use it to set the  public static readonly  Value of field .  This identifier will be used later to refer to dependent properties , Thus, operations such as setting its value or obtaining metadata in a programmatic manner can be realized .
public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);

The front desk

<Grid>
    <local:CustomBorder x:Name="bord" Transparency="{Binding ElementName=slider1, Path=Value}"></local:CustomBorder>
    <Slider Grid.Row="1" HorizontalAlignment="Left" Name="slider1" Width="130" Value="0" Minimum="0" Maximum="255" />
</Grid>

Sample code

DependencyProperties

原网站

版权声明
本文为[zLulus]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212041018646.html