1
votes

I am trying to open a file dialog by pressing a button on view and view call a command "OpenFileDialog" which is defined in viewmodel. That method send a message to view to open the "Microsoft.Win32.OpenFileDialog()" and now I want to communicate the result of OpenFileDialog to ViewModel.

ViewModel

 public ICommand OpenFileCommand {
  get { return new RelayCommand( ( ) => OpenFileCommandExecute( ), ( ) => true ); }
}

private void OpenFileCommandExecute( ) {
  Messenger.Default.Send( "OpenfileDialog" );
}

View

 Messenger.Default.Register( this, "OpenFileDialog", openFileDialog) ;
 private void openFileDialog(  ) {
  OpenFileDialog OFP = new OpenFileDialog( );
  var kk = OFP.ShowDialog( );      
 }

I want to communicate the selected file path to view model. I am using MVVM Light Toolkit and WPF. My code is not in working condition.

1

1 Answers

0
votes

The OpenFileDialog should have a Filename property that you can check after the ShowDialog() method.

Example

OpenFileDialog openDialog = new OpenFileDialog();
openDialog.ShowDialog();
if (string.IsNullOrEmpty(openDialog.Filename)) return;

string path = openDialog.Filename;

Then if you want it accessible on the view model just create your own property for the path in the ViewModel class and populate it with the Filename property.