Dependency Property Overriew

本文详细介绍了WPF中依赖属性(Dependency Property)的概念及其与CLR属性的区别。内容涵盖依赖属性的创建方式、变更通知机制、继承特性及多重提供者支持等核心特性,并提供了具体的代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值