Ryan D'Baisse
Ryan D'Baisse

Reputation: 863

How to Open a WPF Window from Another Application

How can I call a WPF Window from another application? I would like to have a UI.Resources project with all windows, view models, and what I call "sequences" (standard class to control flow of windows). Ultimately, I would call the proper Sequence for the UI process that I want to invoke (Login, GetLocale, etc). The "sequence" class would then create all resources, and handle showing and hiding the correct Windows to accomplish the task. Unfortunately, in the example below, the desired Window never shows. The app just hangs on the ShowDialog() call:

public static bool Process(ClientLibrary client,
                   out Country country, out State state, out City city,
                   out string errorMessage)
{
    country = null;
    state = null;
    city = null;
    errorMessage = null;

    try
    {
        if (client == null) { errorMessage = "Internal Error: Client not supplied"; }

        var model = new LocaleSelectHeirarchyViewModel(client);
        var window = new LocaleSelectHeirarchyWindow(model);

        var result = window.ShowDialog();
        window.Close();
        window = null;

        if (result == null || !result.Value || model.SelectedCity == null)
        {
            return false;
        }

        country = model.SelectedCountry;
        state = model.SelectedState;
        city = model.SelectedCity;

        return true;
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);

        errorMessage = "Internal Error: Client threw an exception";

        return false;
    }
}

Upvotes: 3

Views: 2137

Answers (1)

Sunil Purushothaman
Sunil Purushothaman

Reputation: 9461

I just did something very similar. I would like to share my approach that worked. Here is the approach. We will use System.Windows.Control.DockPanel in the XAML. The dock panel can load different ‘user controls’ dynamically. User control is very similar to a form, on which we can create UI elements. In your case, the contents of the dialogue that you want to show go in here.

Step 1

Place this DockPanel inside a grid.

<DockPanel x:Name="mainDockPanel">          
</DockPanel>

Step 2

The next step is to create the different user controls. Right click on your WPF project in visual studio -> Add new User control

Make sure that the xaml.cs code of the newly added user control has this

public partial class myUserControlUC : UserControl

Essentially it should derive from System.Windows.Controls.UserControl Using the XAML of the user control we can create any fancy UI that we need.

Step 3

Now everything is ready, we just need to call

mainDockPanel.Children.Add(new myUserControlUC());

This can be put inside a switch to show different stuff upon different conditions For example

mainDockPanel.Children.Clear();
            switch (i)
            {
                case 1:                    
                    mainDockPanel.Children.Add(new ToolKitUC1());
                    break;
                case 2:                    
                    mainDockPanel.Children.Add(new ToolKitUC2());
                    break;
                case 3:                    
                    mainDockPanel.Children.Add(new ToolKitUC3());
                    break;
            }

Upvotes: 1

Related Questions