Dependency Property Overriew

来源:互联网 发布:org.apache.http不存在 编辑:程序博客网 时间:2024/05/16 07:35

1)The difference between CLR properties and Dependency Properties

DP支持:Change Notification, Property Value Inheritance, Support for Multiple providers.

结论: DPs are more than just simple CLR properties.

2)如何创建一个Dependency Property

  • Declare a dependencyProperty (Always public static readonly)
  • Initialise the dependencyProperty, either using DependencyProperty.RegisterAttached/DependencyProperty.Register/DependencyProperty.RegisterReadOnly/DependencyPropertyRegisterAttachedReadOnly
  • Declare get/set property wrapper (see note below)
  •  

    public

     

     

    class Button : ButtonBase

    {

     

    public

     

    static readonly DependencyProperty IsDefaultProperty;

     

    public

     

    Button() {DependencyProperty.Register();...}

     

    public

     

    bool IsDefault {

    get {return GetValue(Button.IsDefaultPeroperty);}

     set {SetValue(Button.IsDefaultPeroperty, value); } }

     

     

     

     

     

    }

     

     

    "Although the XAML compiler depends on the wrapper at compile time, at run time WPF calls the underlying GetValue and SetValue methods directly! Therefore, to maintain parity between setting a property in XAML and procedural code, it's crucial that property wrappers DO NOT contain any logic in addition to the GetValue/SetValue calls. If you want to add custom logic, that what the registered callbacks are for. All of WPF's built in property wrapper abide by this rule, so this warning is for anyone writing a custom class with its own dependency properties."

    ==尽管XAML编译器需要WapperProperty,但是运行期间WPF直接调用GetValue/SetValue。所以为了保持一致性,需要在.NET Wrapper Property不要包含任何逻辑.

     

    3)Change Notification

    当DP发生变化是,会自动触发一系列的活动。比如:Property Trigger。通过配置的方式直接实现事件,无需写EventHandler的C# Code.

     

    <Trigger Property="IsMouseOver" Value=“True”>

     <Setter Property="Foreground", Value="Blue" />

    </Trigger>

     

    4)

    含义:属性值会Flowing down the element tree from top to down.

    还有一种特殊形式的Property 是Attached property,允许把Property attached 任意个对象上面。

    比如:TextElement.FontSize;比如StackPanel没有FontSize属性,没有办法inheritence to child property,这时可以用Attached Property。

     

    XAML Code:

     

     

     

    5)Support Multiple Providers.

    Thanks for Change Notification,可以通过4个流程计算出最终的Dependency Propety Value.

    Determine Base Value--------------> Evaluate (if is Expression)------------------->Apply Animations----------->Coerce---->Validate.

     

    其中Determine Base Value,最高到最低的优先顺序为:

    Local Value->Style Trigger->Template Trigger->Style Setters->Theme Style triggers->Theme style setters->Property Value inheritance->Defalut Value.

    根据这个就解释了Status Bar的FontSize为什么没有从Stack Panel得到值,Style Setters.

     

     

    DepedencyProperty(一般放在Custom Control), Default Value:

    确保类型一致,

    DependencyProperty.Register("Value"typeof (decimal), typeof (MainWindow), new PropertyMetadata(10)

    会发生异常!!!

    this.DataContext = this/ViewModel;

     <TextBlock Text="{Binding Path=Value}" Grid.Row="0"/>
    public decimal Value
            {
                get
                {
                    return Convert.ToDecimal(base.GetValue(ValueProperty));
                }
                set
                {
    //不能有任何的逻辑                
    base.SetValue(ValueProperty,value);
                    
                }
            }

            public static DependencyProperty
                ValueProperty = DependencyProperty.Register("Value"typeof (decimal), typeof (MainWindow),
                                                            new PropertyMetadata(Convert.ToDecimal(10)));
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Value += 1;
            }