1

I have a WPF (Prism) application that needs "select directory" functionality. The main window's menu opens a child "Settings" window through notorious Prism InteractionRequest. That "Settings" window has a button to open a "Select Directory" window.

Since the standard FolderBrowserDialog dialog is so ugly, I tried to use a CommonOpenFileDialog from Windows API Code Pack.

I use a service from Orc.Controls to wrap the dialog:

using Microsoft.WindowsAPICodePack.Dialogs;

public class MicrosoftApiSelectDirectoryService : ISelectDirectoryService
{
    public bool DetermineDirectory()
    {
        var browserDialog = new CommonOpenFileDialog();
        browserDialog.IsFolderPicker = true;
        browserDialog.Title = Title;
        browserDialog.InitialDirectory = InitialDirectory;

        if (browserDialog.ShowDialog() == CommonFileDialogResult.Ok)
        {
            DirectoryName = browserDialog.FileName;
            return true;
        }

        DirectoryName = string.Empty;
        return false;
    }
// ...
}

From my view model, I call _selectDirectoryService.DetermineDirectory():

    public DelegateCommand SelectDirectoryCommand { get; private set; }

    private void SelectDirectory()
    {
        if (_selectDirectoryService.DetermineDirectory())
        {
            var dir = _selectDirectoryService.DirectoryName;
            // ...
        }
    }

The problem is that after selecting a directory in a CommonOpenFileDialog, the "Settings" window loses focus (and for some reason actually hides behind the maximized main window). By contrast, the FolderBrowserDialog returns focus to the "Settings" window.

So, basically I need either a better "select directory" implementation or I need to find a way to focus the "Settings" window again without severe violation of MVVM pattern. Any ideas would be appreciated.

mechanic
  • 761
  • 6
  • 11
  • Its a lot of work to create your own directory browser, especially if you want it to have all the functionality that the Windows File Browser has. So I'd recommend against that. For me, I see a DirectoryBrowser as a UI control and part of the UI layer of my applications, so often have my related code in the View's code-behind. Is that an option for you here? Setting Focus is also the responsibility of the View layer in my opinion. If you really consider it part of the ViewModel layer though, I'd suggest some kind of DialogService for showing/hiding dialogs correctly within the View – Rachel Oct 18 '16 at 15:36

1 Answers1

0

You have to set the parent/owner of the Window to prevent it from suddenly popping behind it.

  • What do you mean? The main window opens the "Settings" window through prism:InteractionRequestTrigger (prism:PopupWindowAction). The "Settings" window opens the CommonOpenFileDialog through a command calling a service. But the CommonOpenFileDialog doesn't return focus to the "Settings" window when closed. – mechanic Oct 18 '16 at 16:18
  • I mean what I said. You need to modify your service to set the owner of the dialog window. –  Oct 18 '16 at 16:35
  • 1
    As a dirty hack, I put some code behind: Window parentWindow = Window.GetWindow(this); if (browserDialog.ShowDialog(parentWindow) == CommonFileDialogResult.Ok) { profileDirectory.Text = browserDialog.FileName; // ..; } It kinda solved the lost focus problem, but violates MVVM. I wonder if there is a better way to pass a parent window handle to the service. – mechanic Oct 18 '16 at 17:35