收藏更改样本

发布于 2024-10-10 19:01:58 字数 84 浏览 2 评论 0原文

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

Can someone point to an example where CollectionChanged is implemented. I am using wpf mvvm light. I tried to google, didn't find anything good enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-10-17 19:01:58
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 的详细信息,请参阅

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

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;
}

For more information about the NotifyCollectionChangedEventArgs look here.

EDIT: Because you need a list of added/removed items, I modified the sample code.

安稳善良 2024-10-17 19:01:58
 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);
    }

这里是一个非常简单的完整示例

 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文