WPF dependency property

WPF provides a set of services that can be used to extend the functionality (CLR) property of the common language runtime. These services are often referred to as the WPF property system. System supported by the WPF property attribute is called dependency properties.

Why should I use dependency properties?

A scenario: a screen 100 has buttons, each button 100 has attributes, the system needs to allocate memory attribute 10000, but most of the properties are at the initial default value.

Scene Two: If the parent class Background button black, button style of red Background, Background Properties button is bound to another button background color (blue), while the button is playing an animation, Background from yellow to green , the button displays the background color in the end is what color, animation would play after what color?

Dependency scene property used

  1. We want to set the properties in a style
  2. We want the property supports data binding
  3. We want to use dynamic resource references set properties
  4. I want to automatically inherit property values ​​from the parent element tree
  5. Hope properties can be animated
  6. When the report want the property in the operating system property, or the environment or the user to perform read and use the style to change the attributes of the previous value
  7. Want to use established, the process also uses metadata WPF conventions, such as whether to require reporting system layout change the property value to rewrite elements of visual objects

Custom Dependency Properties

1, the class inherits from DependencyObject
2, defines a static objects DependencyProperty

public static readonly DependencyProperty IsSpinningProperty = 
      DependencyProperty.Register( "IsSpinning", typeof(Boolean), ... );

 
public bool IsSpinning
{ 
      get { return (bool)GetValue(IsSpinningProperty); } 
      set { SetValue(IsSpinningProperty, value); } 
} 

Dependency property metadata

You can specify the metadata (PropertyMetadata) when registering a dependency property.

1, the default value of
2, property change callbacks
3, cast callback
4, whether the metadata can be modified
5, whether to disable the animation on the attribute
6, whether it will affect the arrangement layout of the system
7, whether it will affect the measurement system layout
8, will affect the layout of the system arrangement of the parent element
9, the measurement will affect the layout of the parent element of the system
10, will affect the redraw element
11, whether the default way binding
12, get or set in the application having the default value of the binding properties of the metadata to be used UpdateSourceTrigger these bindings UpdateSourceTrigger to default.
13, whether the element can be inherited by a child element
14, whether to support data binding

Use dependency property

Dynamic Resource -Background="{DynamicResource MyBrush}"

Data Binding -Background="{Binding MyBrush}"

style--<Setter Property="Background" Value="Red"/>

Animation -<ColorAnimation Storyboard.TargetName="MyButton" Storyboard.TargetProperty="Background" From="Red" To="Blue"/>

Rewriting metadata - overwriting the default attributes of the parent class behavior dependent property OverrideMetadata

Property value inheritance - inheritance attribute value of parent element tree (DataContext, FontSize)

Design of integrated WPF - using the WPF designer can edit the attribute value in the properties window

Dependency property precedence

Firstly the code below

<Button Background="Red" Content="Click Me!">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Green"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

priority:

1, the default value of the attribute from the dependent metadata
2, inheritance
3, the default (themes) Style
4, Style Setter
. 5, the trigger templates
6, style trigger
7, the implicit style
8, TemplatedParent template attributes
9, the local value
10, Hold animation activity or have behavioral animation
11, system attributes cast

Additional attributes of the attribute dependency

Additional property is a special property of dependency, its purpose is to allow a different sub-elements is actually defined in the parent element specified attribute value unique

DependencyProperty.RegisterAttached

1, and is defined in the parent element design additional properties. Thereafter, the parent element loops through its child elements, obtaining the value, and applying these values ​​in some way, such as Grid.Row, DockPanel.Dock, Canvas.Top.

2, to define additional attributes used as various types of possible parent element and the child content model, such as ScrollViewer. HorizontalScrollBarVisibility.

3, the definition of an additional attribute type represents a service. Other types of set values ​​for the attached property, then, when calculating the elements of the attribute set in the context of a service, acquired by the attribute value of the additional internal logic of the service class.

Collection type dependency property

Code analysis:

public class Aquarium : DependencyObject
{
    private static readonly DependencyPropertyKey AquariumContentsPropertyKey =
        DependencyProperty.RegisterReadOnly(
            "AquariumContents",
            typeof(List<FrameworkElement>),
            typeof(Aquarium),
            new FrameworkPropertyMetadata(new List<FrameworkElement>())
            );

    public static readonly DependencyProperty AquariumContentsProperty =
        AquariumContentsPropertyKey.DependencyProperty;

    public List<FrameworkElement> AquariumContents
    {
        get { return (List<FrameworkElement>)GetValue(AquariumContentsProperty); }
    }
}


var myAq1 = new Aquarium();
var myAq2 = new Aquarium();
var e1 = new Button();
var e2 = new Button();
myAq1.AquariumContents.Add(e1);
myAq2.AquariumContents.Add(e2);
MessageBox.Show("aq1 contains " + myAq1.AquariumContents.Count + " things");
MessageBox.Show("aq2 contains " + myAq2.AquariumContents.Count + " things");


public Aquarium()
{
    SetValue(AquariumContentsPropertyKey, new List<FrameworkElement>());
}

Source: Shenzhen Star Technology building

Guess you like

Origin www.cnblogs.com/ghhjanes/p/11261011.html