简体   繁体   English

更新绑定到可观察集合的组合框

[英]Updating Combobox bound to an observable collection

I'm binding observablecColletion in ViewModel to a combobox in View (using MVVM Caliburn.Micro). 我将ViewModel中的observablecColletion绑定到View中的组合框(使用MVVM Caliburn.Micro)。 The obsevableColletion is getting its items from a string array which gets the file names in a directory. obsevableColletion从一个字符串数组获取其项目,该字符串数组获取目录中的文件名。 So my question is: How can i make the combobox to update automatially when i add or delete a file in the directory? 所以我的问题是:当我在目录中添加或删除文件时,如何使组合框自动更新?

Here is my code: ViewModel 这是我的代码: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);
   }
}

View: 视图:

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

Here's a view model that does what you want. 这是一个可以满足您需求的视图模型。 It uses a FileSystemWatcher to detect changes in the file system. 它使用FileSystemWatcher来检测文件系统中的更改。 It uses Dispatcher to marshal the FileSystemWatcher events back to the UI thread (you don't want to change an ObservableCollection on background threads). 它使用DispatcherFileSystemWatcher事件编组回UI线程(您不想在后台线程上更改ObservableCollection )。 You should call Dispose of this view model when you are done with it. 完成使用此视图模型后,应调用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);
            }));
    }
}

You've to bind an event to listen to changes in the directory you're adding/removing stuff from. 您必须绑定一个事件以侦听要在其中添加/删除内容的目录中的更改。 Just setup FileSystemWatcher and add/remove stuff from your observable collection as it alerts you. 只需设置FileSystemWatcher并在它提醒您时从可观察的集合中添加/删除内容。

Example at http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx http://msdn.microsoft.com/zh-cn/library/system.io.filesystemwatcher(v=vs.110).aspx上的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM