Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实现OnCollectionChanged,以便ListBox自动更新WPF_C#_Wpf_Listbox_Observablecollection_Inotifycollectionchanged - Fatal编程技术网

C# 实现OnCollectionChanged,以便ListBox自动更新WPF

C# 实现OnCollectionChanged,以便ListBox自动更新WPF,c#,wpf,listbox,observablecollection,inotifycollectionchanged,C#,Wpf,Listbox,Observablecollection,Inotifycollectionchanged,我有这个对象包装器,我用它的实例填充集合: public class Multimedia : INotifyPropertyChanged { //... constructor //... getters and setters for the properties public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { Observable

我有这个对象包装器,我用它的实例填充集合:

public class Multimedia : INotifyPropertyChanged
{
   //... constructor
   //... getters and setters for the properties

   public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
   {
      ObservableCollection<Multimedia> objSender = sender as ObservableCollection<Multimedia>;
      NotifyCollectionChangedAction action = e.Action;
   }
}
XAML:



您不需要在
多媒体
中更改
OnCollectionChanged
,而且
ObservableCollection
已经实现了
INotifyCollectionChanged
。看来问题出在别的地方了。你能为
ListBox
显示你的XAML和绑定吗?@dkozl-我更新了我的问题帖子。当你添加时,你使用的是相同的
mediaList
?是否异步添加?不,我没有异步方法。我没有完全理解你的评论。你是说当我添加
addMedia()
方法时?您的意思是,我将新条目添加到
mediaList
集合的另一个实例中吗?我的意思是,构造函数是您执行
mediaList=new MultiMediaList()操作的唯一位置之后,您只需执行
mediaList.addMedia(…)
。您能展示一下如何添加项目吗?
public class MultiMediaList : ObservableCollection<Multimedia>
{
    //... constructor with creating several default objects of Multimedia

    public void addMedia(string title, string artist, string genre, MediaType type)
    {
        this.Add(new Multimedia(title, artist, genre, type));
    }
}
public partial class MainWindow : Window
{
    MultiMediaList mediaList;
    public MainWindow()
    {
        InitializeComponent();
        mediaList = new MultiMediaList();
        LB_media.ItemsSource = mediaList;
    }

    //...
}
<ListBox Name="LB_media" DisplayMemberPath="Title" ... />