现在的位置: 首页 > 综合 > 正文

WPF-InotifyPropertyChanged

2013年05月29日 ⁄ 综合 ⁄ 共 2675字 ⁄ 字号 评论关闭

InotifyPropertyChanged

List<T>与observablecollection<T> 的区别,,如何选择使用哪 一个?

对于List<T>

      若 T 不实现 INotifyPropertyChanged, List不实现 INotifyCollectionChanged,则它只是一个普通的容器
对于ObservableCollection<T>

    它本身就已经实现了 INotifyCollectionChanged ,,若再给T 实现INotifyPropertyChanged 接口,,则功能齐全,,当然也消耗性能

  INotifyPropertyChanged是什么,它有什么作用?通过查阅MSDN我们知道,INotifyPropertyChanged接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。
   当绑定数据源的某属性值改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,INotifyPropertyChanged确实是一个强大的接口。
  首先,我们需要了解如下有关Silverlight 2.0 数据绑定的术语:

  Binding - 将绑定目标对象的属性与数据源联接起来
  Source - 绑定的数据源
  Mode - 绑定的数据流的方向 [System.Windows.Data.BindingMode枚举]
  BindingMode.OneTime - 一次绑定。创建绑定时一次性地更新绑定目标对象的属性
  BindingMode.OneWay - 单向绑定(默认值)。数据源的改变会自动通知到绑定目标对象的属性
  BindingMode.TwoWay - 双向绑定。数据源或绑定目标对象的属性的值发生改变时会互相通知。显然,做数据验证的话一定要是双向绑定
  INotifyPropertyChanged - 向客户端发出某一属性值已更改的通知
  IValueConverter - 值转换接口,将一个类型的值转换为另一个类型的值。它提供了一种将自定义逻辑应用于绑定的方式

接下来我们学习简单的数据绑定并了解INotifyPropertyChanged所发挥的作用。
   首先启动VS2008,新建Silverlight应用程序,程序名为MyINotifyPropertyChanged,系统自动为我们建立两个项目,一个是MyINotifyPropertyChanged,一个是MyINotifyPropertyChanged.Web。在MyINotifyPropertyChanged子项目下,我们添加了两类数据源:
   StudentNotify.cs :引入了INotifyPropertyChanged接口

 using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;  //需要添加此命名空间

namespace MyINotifyPropertyChanged
{
    public class StudentNotify : INotifyPropertyChanged
    {
        private string NameValue;
        private int AgeValue;

        public string SName
        {
            get { return NameValue; }
            set
            {
                NameValue = value;
                // Call NotifyPropertyChanged when the property is updated
                NotifyPropertyChanged("SName");
            }
        }
        public int  SAge
        {
            get { return AgeValue; }
            set
            {
                AgeValue = value;
                // Call NotifyPropertyChanged when the property is updated
                NotifyPropertyChanged("SAge");
            }
        }


        // Declare the PropertyChanged event
        public event PropertyChangedEventHandler PropertyChanged;

        // NotifyPropertyChanged will raise the PropertyChanged event passing the
        // source property that is being updated.
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public StudentNotify()
        {
        }
        public StudentNotify(string NameStr,int AgeInt)
        {
            SName = NameStr;
            SAge = AgeInt;
        }
    }
}

 Student.cs      :没有引入INotifyPropertyChanged接口

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyINotifyPropertyChanged
{
    public class Student
    {
        public string SName { get; set; }
        public int SAge { get; set; }
    }
}

 

 

抱歉!评论已关闭.