使用MVVM在ListBox中添加项目时发生的事件

问题描述 投票:-1回答:1

将项目添加到ListBox的项目源时,我需要绑定命令。但是listBox没有项目源更改的事件。我已经尝试创建一个自定义控件或一个自定义控件,但是它没有任何改变。谢谢您的帮助!

c# wpf mvvm listbox
1个回答
0
投票

您可以为此目的在ObservableCollection上添加CollectionChanged处理程序

public class MyViewModel
{
    public MyViewModel()
    {
        ListBoxItems.CollectionChanged += ListBoxItemsOnCollectionChanged;
    }

    private void ListBoxItemsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {

    }

    public ObservableCollection<string> ListBoxItems { get; } = new ObservableCollection<string>();
}
© www.soinside.com 2019 - 2024. All rights reserved.