WPF 普通屬性變化通知


問題描述:使用ObservableCollection<OrderItem> source 給Datagrid.ItemsSource賦值,在后台更新source集合后,前台Datagrid對應的單元格數據只有進行編輯模式才會獲得更新后的Source數據。

問題解決:如下代碼,數據源Model實現INotifyPropertyChanged接口,即可做到實時通知

注:ObservableCollection<OrderItem> source  替換為  List<OrderItem> 也可實現實時通知

 

    public class OrderItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        private OpeType state;
        public OpeType State
        {
            get
            {
                return state;
            }
            set
            {
                state = value;
                NotifyPropertyChanged("State");
            }
        }

        ......
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM