【问题标题】:CollectionChanged sampleCollectionChanged 样本
【发布时间】:2011-06-03 00:19:53
【问题描述】:

有人可以指出实现 CollectionChanged 的​​示例。我正在使用 wpf mvvm light。我尝试谷歌,没有找到足够好的东西。

【问题讨论】:

  • 请详细一点。你想做什么?
  • 我有一个启用了内联编辑的 observableCollection itemsource 的数据网格,当用户完成编辑/添加/删除此数据网格中的数据时,我想将该数据发送到 web 服务进行更新。
  • 我不敢相信你已经接受了下面的 Arxisos 答案......下面的代码没有意义......如何实现 NotifyCollectionChanged 和 NotifyCollectionChangedEventArgs?在这里查看blog.stephencleary.com/2009/07/… 以获得良好的简短描述,还有指向实现和更多详细信息的链接。

标签: wpf mvvm mvvm-light


【解决方案1】:
 public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

    }
    private ObservableCollection<Person> persons = new ObservableCollection<Person>();


    public MainWindow()
    {
        InitializeComponent();
        dgPerson.ItemsSource = persons;
        persons.CollectionChanged += this.OnCollectionChanged;
    }


    void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        //Get the sender observable collection
        ObservableCollection<Person> obsSender = sender as ObservableCollection<Person>;
        NotifyCollectionChangedAction action = e.Action;
        if (action == NotifyCollectionChangedAction.Add)
            lblStatus.Content = "New person added";
        if (action == NotifyCollectionChangedAction.Remove)
            lblStatus.Content = "Person deleted";
    }
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        Person p = new Person();
        p.FirstName = txtFname.Text;
        p.LastName = txtLname.Text;

        persons.Add(p);
    }

A very simple complete example here

【讨论】:

    【解决方案2】:
    public ObservableCollection<string> Names { get; set; }
    
    public ViewModel()
    {
       names = new ObservableCollection<string>();
       Names.CollectionChanged += this.OnCollectionChanged;
    }
    
    void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
       //Get the sender observable collection
       ObservableCollection<string> obsSender = sender as ObservableCollection<string>;
    
       List<string> editedOrRemovedItems = new List<string>();
       foreach(string newItem in e.NewItems)
       {
           editedOrRemovedItems.Add(newItem);
       }
    
       foreach(string oldItem in e.OldItems)
       {
           editedOrRemovedItems.Add(oldItem);
       }
    
       //Get the action which raised the collection changed event
       NotifyCollectionChangedAction action = e.Action;
    }
    

    有关 NotifyCollectionChangedEventArgs 的更多信息,请查看here

    编辑:因为您需要添加/删除项目的列表,所以我修改了示例代码。

    【讨论】:

    • 出于某种原因,仅在添加或删除项目的情况下才会调用 onCollectionChanged 方法。但不是在编辑项目的情况下。我也想知道如何继续进行,我的意思是如何最终在单个集合中发送新添加、删除和编辑的项目的列表/集合。谢谢。
    • 听起来您还有其他问题要发布。
    • ObservableCollection 不提供在编辑元素上调用 OnCollectionChanged 的​​功能。你必须自己做。集合中的项目必须实现 INotifyPropertyChanged,并且必须将事件侦听器添加到 INotifyPropertyChanged 的​​ PropertyChanged 事件。查看stackoverflow.com/questions/269073/… 以获取更多信息。
    • 如果您对此有疑问:创建一个新问题,因为这不属于该问题。
    猜你喜欢
    • 2011-06-03
    • 2023-04-09
    • 2021-08-16
    • 2017-12-06
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多