I always did such things in ViewModel
My ParentViewModel will contain an instance of OpenFileViewModel , and when ParentViewModel.SelectFileCommand is executed, it calls something like OpenFileViewModel.SelectFile()
To get the selected file, I often subscribe to OpenFileViewModel.PropertyChanged and listen to the change events in the FileName property, or sometimes I will have a rewritable ProcessFile method that I can connect to the event that fires when a file is selected.
The OpenFileViewModel.SelectFile method usually looks something like this:
private void SelectFile() { var dlg = new OpenFileDialog(); dlg.DefaultExt = this.Extension; dlg.Filter = this.Filter; if (dlg.ShowDialog() == true) { var file = new FileInfo(dlg.FileName); FileName = file.FullName; if (ProcessFileDelegate != null) ProcessFileDelegate() } }
and my ParentViewModel will often contain code that looks something like this:
public ParentViewModel() { this.OpenFileDialog = new OpenFileViewModel(); this.OpenFileDialog.PropertyChanged += OpenFileDialog_PropertyChanged; this.OpenFileDialog.ProcessFileDelegate = ProcessFile; }
source share