何时使用 WPF 依赖属性与 INotifyPropertyChanged [英] When to use a WPF Dependency Property versus INotifyPropertyChanged

查看:26
本文介绍了何时使用 WPF 依赖属性与 INotifyPropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们是否对在视图模型中触发 INotifyPropertyChanged.PropertyChanged 的简单 .NET 属性是否足够有任何指导?那么你什么时候想升级到一个完整的依赖属性?还是 DP 主要用于视图?

Do folks have any guidance on when a simple .NET property that fires INotifyPropertyChanged.PropertyChanged is sufficient in a view model? Then when do you want to move up to a full blown dependency property? Or are the DPs intended primarily for views?

推荐答案

有几种方法:

1.依赖属性

当您使用依赖项属性时,它在具有视觉外观(UIElements)的元素类中最有意义.

While you using the dependency property it makes the most sense in elements-classes that have a visual appearance (UIElements).

优点:

  • WPF 为您做逻辑工作
  • 某些机制(如动画)仅使用依赖属性
  • '适合'ViewModel 样式

缺点:

  • 您需要导出表单DependencyObject
  • 对于简单的东西有点尴尬

示例:

public static class StoryBoardHelper
{
    public static DependencyObject GetTarget(Timeline timeline)
    {
        if (timeline == null)
            throw new ArgumentNullException("timeline");

        return timeline.GetValue(TargetProperty) as DependencyObject;
    }

    public static void SetTarget(Timeline timeline, DependencyObject value)
    {
        if (timeline == null)
            throw new ArgumentNullException("timeline");

        timeline.SetValue(TargetProperty, value);
    }

    public static readonly DependencyProperty TargetProperty =
            DependencyProperty.RegisterAttached(
                    "Target",
                    typeof(DependencyObject),
                    typeof(Timeline),
                    new PropertyMetadata(null, OnTargetPropertyChanged));

    private static void OnTargetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Storyboard.SetTarget(d as Timeline, e.NewValue as DependencyObject);
    }
}

2.System.ComponentModel.INotifyPropertyChanged

通常,在创建数据对象时,您会使用这种方法.对于类似数据的东西来说,这是一个简单而简洁的解决方案.

Usually, when creating a data object, you’ll use this approach. It is simple and neat solution for Data-like stuff.

优点和缺点 - 对 1 的补充.您只需实现一个事件 (PropertyChanged).

Pros and Cons - complementary to 1. You need to to implement only one event (PropertyChanged).

示例:

public class Student : INotifyPropertyChanged 
{ 
   public event PropertyChangedEventHandler PropertyChanged; 
   public void OnPropertyChanged(PropertyChangedEventArgs e) 
   { 
       if (PropertyChanged != null) 
          PropertyChanged(this, e); 
   } 
}

private string name; 
public string Name; 
{ 
    get { return name; } 
    set { 
           name = value; 
           OnPropertyChanged(new PropertyChangedEventArgs("Name")); 
        } 
} 

3.PropertyName已更改

为具有指定名称的每个属性引发事件(例如 NameChanged).事件必须具有此名称,由您来处理/提升它们.与2类似的方法.

Rising an event for each property with specified name(f.e. NameChanged). Event must have this name and it is up to you to handle/rise them. Similar approach as 2.

4.获取绑定

使用 FrameworkElement.GetBindingExpression() 你可以获得 BindingExpression 对象并调用 BindingExpression.UpdateTarget() 进行刷新.

Using the FrameworkElement.GetBindingExpression() you can get the BindingExpression object and call BindingExpression.UpdateTarget() to refresh.

第一个和第二个最有可能取决于您的目标.

First and second are the most likely depending what is your goal.

总而言之,这是视觉与数据.

All in all, it is Visual vs Data.

这篇关于何时使用 WPF 依赖属性与 INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆