首页 > 代码库 > WPF 修改数据后更新UI

WPF 修改数据后更新UI

ObservableCollection<T> 只有项添加或删除才会更新UI

要想属性发生变动后立刻更新到UI,必须继承 INotifyPropertyChanged 接口,示例如下
public class SurfaceDetail: INotifyPropertyChanged
   {
       //不更新到界面的属性
       public string name { get; set; }
 
      //以下是更新到界面的属性 
 
       private string _color;
 
       public string color
       {
           get
           {
               return _color;
           }
 
           set
           {
               _color = value;
               OnPropertyChanged("color");
           }
       } 
 
       public event PropertyChangedEventHandler PropertyChanged;
       private void OnPropertyChanged(string propertyName)
       {
           PropertyChangedEventHandler handler = this.PropertyChanged;
           if (handler != null)
           {
               handler(this, new PropertyChangedEventArgs(propertyName));
           }
       }
   }

  

WPF 修改数据后更新UI