If you're determined to use FolderBrowserDialog, I'd use this kind of design.
First, create a DependencyProperty on your View to expose its handle.
public static readonly DependencyProperty WindowHandleProperty =
DependencyProperty.Register("WindowHandle", typeof(System.Windows.Forms.IWin32Window), typeof(MainWindow), new PropertyMetadata(null));
// MainWindow.cs
public System.Windows.Forms.IWin32Window WindowHandle
{
get { return (System.Windows.Forms.IWin32Window)GetValue(WindowHandleProperty); }
set { SetValue(WindowHandleProperty, value); }
}
Now, when your window loads, you can retrieve the handle using the extensions provided in the question you linked to:
// MainWindow.cs
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var binding = new Binding();
binding.Path = new PropertyPath("WindowHandle");
binding.Mode = BindingMode.OneWayToSource;
SetBinding(WindowHandleProperty, binding);
WindowHandle = this.GetIWin32Window();
}
So, you are binding one-way to source using a "WindowHandle" property. So if your ViewModel has a WindowHandle property, it will be kept up to date with the valid IWin32Handle for the related view:
// ViewModel.cs
private System.Windows.Forms.IWin32Window _windowHandle;
public System.Windows.Forms.IWin32Window WindowHandle
{
get
{
return _windowHandle;
}
set
{
if (_windowHandle != value)
{
_windowHandle = value;
RaisePropertyChanged("WindowHandle");
}
}
}
This is a good solution because you're not hard-coding one ViewModel to be paired with one specific View. If your use multiple Views with the same ViewModel, it should just work. If you create a new View but you don't implement the DependencyProperty, it will just operate with a null handle.
EDIT:
As a side note, have you actually tested just not providing an IWin32Owner parameter? For me, it still automatically opens as a modal dialog for the application and blocks the user from interacting with all of the application's windows. Is there something else you need it to do instead?