Question

I'm trying to use a FolderBrowserDialog in my WPF applciation to dictate source and destination folder paths. Currently, I have a bit of a hack as my solution:

// Opens the FolderBrowserDialog and gets the result.
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();

// Updates the TextBox with the chosen folder path.
srcPathTextBox.Text = dialog.SelectedPath;

However, I am trying to figure out a more elegant solution, which is proving to be difficult since I am still fairly new to WPF. I know that data binding is a popular method of automatically updating UI elements in WPF, but since the folder path is chosen from a dialog box I do not know if data binding is a viable option.

Any help would be greatly appreciated.

Was it helpful?

Solution

You need to implement the INotifyPropertyChanged on your viewmodel and then assign the value returned from the SelectedPath variable to a public string on your viewmodel. But raising the PropertyChanged event from the string setter, the UI will update the textbox. You'll need to set the datasource of your view to the view model, but all of this is explained in countless articles on MVVM design and WPF.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top