更新Combobox绑定到可观察的集合

时间:2014-03-18 18:51:33

标签: c# wpf mvvm caliburn.micro

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

这是我的代码: 视图模型

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" ......../>

2 个答案:

答案 0 :(得分:1)

这是一个可以满足您需求的视图模型。它使用FileSystemWatcher来检测文件系统中的更改。它使用DispatcherFileSystemWatcher事件封送回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);
            }));
    }
}

答案 1 :(得分:0)

您必须绑定一个事件来收听您添加/删除内容的目录中的更改 从。只需设置FileSystemWatcher并添加/删除可观察集合中的内容,因为它会提醒您。

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

的示例