正在更新绑定到一个可观察集合的组合框

本文关键字:观察 一个 集合 组合 更新 绑定 | 更新日期: 2023-09-27 17:50:47

我将ViewModel中的observableccollection绑定到View中的一个组合框(使用MVVM Caliburn.Micro)。obsevableColletion从一个字符串数组中获取它的项,该字符串数组获取目录中的文件名。所以我的问题是:当我在目录中添加或删除文件时,如何使组合框自动更新?

下面是我的代码:ViewModel
private ObservableCollection<string> _combodata;
Public ObservableCollection<string> ComboData
{
    get
   {
      string[] files = Directory.GetFiles("C:''Documents");
      _combodata = new ObservableCollection<string>(files);
      return _combodata;
   }
   set
   {
       _combodata = value;
       NotifyOfPropertyChange(() => ComboData);
   }
}

视图:

<ComboBox x:name = "ComboData" ......../>

正在更新绑定到一个可观察集合的组合框

这是一个你想要的视图模型。它使用FileSystemWatcher来检测文件系统中的更改。它使用Dispatcher将FileSystemWatcher事件封送回UI线程(您不希望更改后台线程上的ObservableCollection)。当你用完这个视图模型后,你应该调用Dispose。

public class ViewModel : IDisposable
{
    // Constructor.
    public ViewModel()
    {
        // capture dispatcher for current thread (model should be created on UI thread)
        _dispatcher = Dispatcher.CurrentDispatcher;
        // start watching file system
        _watcher = new FileSystemWatcher("C:''Documents");
        _watcher.Created += Watcher_Created;
        _watcher.Deleted += Watcher_Deleted;
        _watcher.Renamed += Watcher_Renamed;
        _watcher.EnableRaisingEvents = true;
        // initialize combo data
        _comboData = new ObservableCollection<string>(Directory.GetFiles(_watcher.Path));
        ComboData = new ReadOnlyObservableCollection<string>(_comboData);
    }
    // Disposal
    public void Dispose()
    {
        // dispose of the watcher
        _watcher.Dispose();
    }
    // the dispatcher is used to marshal events to the UI thread
    private readonly Dispatcher _dispatcher;
    // the watcher is used to track file system changes
    private readonly FileSystemWatcher _watcher;
    // the backing field for the ComboData property
    private readonly ObservableCollection<string> _comboData;
    // the ComboData property should be bound to the UI
    public ReadOnlyObservableCollection<string> ComboData { get; private set; }
    // called on a background thread when a file/directory is created
    private void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        _dispatcher.BeginInvoke(new Action(() => _comboData.Add(e.FullPath)));
    }
    // called on a background thread when a file/directory is deleted
    private void Watcher_Deleted(object sender, FileSystemEventArgs e)
    {
        _dispatcher.BeginInvoke(new Action(() => _comboData.Remove(e.FullPath)));
    }
    // called on a background thread when a file/directory is renamed
    private void Watcher_Renamed(object sender, RenamedEventArgs e)
    {
        _dispatcher.BeginInvoke(new Action(() =>
            {
                _comboData.Remove(e.OldFullPath);
                _comboData.Add(e.FullPath);
            }));
    }
}

您必须绑定一个事件来侦听正在添加/删除内容的目录中的更改从。只要设置FileSystemWatcher和添加/删除东西从你的可观察集合,因为它提醒你。

示例:http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx