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

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

问题描述

千万人有当触发一个简单的.NET属性 INotifyPropertyChanged.PropertyChanged 上的任何指导,是在一个视图模型是否足够?然后,当你想向上移动到一个完全成熟的依赖属性?或者是主要针对意见DPS的?

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。依赖属性

当你使用它使(的UIElement S)元素类具有视觉外观。

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

优点:


  • WPF做逻辑的东西对你

  • 某些机制喜欢动漫只使用依赖属性

  • '适合'视图模型风格

缺点:


  • 您需要派生形式的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 更改

瑞星具有指定名称每个属性的事件(f.e。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.GetBindingEx pression()就可以得到 BindingEx pression 对象
并调用 BindingEx pression.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.

所有的一切,这是视觉VS数据。

All in all, it is Visual vs Data.

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

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