mechanic
mechanic

Reputation: 781

A WPF/Prism window calling CommonOpenFileDialog (select directory) loses focus after selection

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.

Upvotes: 1

Views: 1284

Answers (1)

user5420778
user5420778

Reputation:

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

Upvotes: 0

Related Questions