簡體   English   中英

子財產的收藏更改事件

[英]Collection changed event for child property

我使用下面的代碼段來創建綁定到DataGrid的ObservableCollection。

public  class Test:INotifyPropertyChanged
    {

        private string _name;
        public  string Name
        {
            get { return _name; }
            set { _name = value;OnpropertyChanged("Name"); }
        }

        private string _city;
        public  string City
        {
            get { return _city; }
            set
            {
                _city = value;OnpropertyChanged("City");}
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnpropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
        #endregion
    }

class Data:INotifyPropertyChanged
    {

        private int customerID;

        public int CustomerID
        {
            get { return customerID; }
            set { customerID = value; OnpropertyChanged("CustomerID"); }
        }

        private bool isSelected;

        public  bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; OnpropertyChanged("IsSelected"); }
        }

        private ObservableCollection<Test> _collection;
        public ObservableCollection<Test> Collection
        {
            get { return _collection; }
            set { _collection = value;OnpropertyChanged("Collection" +
                                                        ""); }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnpropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }


    }
class ViewModel:NotificationObject
{
    public ViewModel()
    {
        this.GDCSource = Getsource();
    }

    private ObservableCollection<Data> _gdcsource;

    public ObservableCollection<Data> GDCSource
    {
        get { return _gdcsource; }
        set { _gdcsource = value; RaisePropertyChanged("GDCSource");}
    }




    private ObservableCollection<Data> Getsource()
    {
        ObservableCollection<Data> items = new ObservableCollection<Data>();
        if (items != null)
        {
            items.Add(new Data()
                {
                    IsSelected = true,
                    CustomerID = 1,
                });
            items.Add(new Data()
                {
                    IsSelected = true,
                    CustomerID = 2,
                });
        }
        return items;

    }
}

 public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            ViewModel vmModel = new ViewModel();
            this.datagrid.ItemsSource = vmModel.GDCSource;
            vmModel.GDCSource.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(GDCSource_CollectionChanged);
        }

        void GDCSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //Listen the collection changed event for underlying source
        }

        // add the object to the Collection property
        private void Test_OnClick(object sender, RoutedEventArgs e)
        {
            (this.DataContext as ViewModel).GDCSource[0].Collection.Add(new Test() { Name = "Name1", City = "City1" });
            (this.DataContext as ViewModel).GDCSource[0].Collection.Add(new Test() { Name = "Name1", City = "City1" });
            (this.DataContext as ViewModel).GDCSource[0].Collection.Add(new Test() { Name = "Name1", City = "City1" });
        }
    }

在任何情況下都可以在添加Collection屬性的同時進行監聽。

提前致謝

此致Rajasekar

如果您是想注冊可觀察集合中添加/刪除項目時引發的事件,則應查看CollectionChanged事件

ObservableCollection<T>.CollectionChanged Event

在添加,刪除,更改,移動項目或刷新整個列表時發生。

您可以根據需要擴展自己的ObservableCollection版本,並覆蓋add方法,

您可以在此處解雇任何代表或您想注冊的任何對象,UI會使用ObservableCollection自動更新,其中添加/刪除的項目您不需要為此做任何事情,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM