83

How do you display a custom UserControl as a dialog in C#/WPF (.NET 3.5)?

7 Answers 7

162

Place it in a Window and call Window.ShowDialog. (Also, add references to: PresentationCore, WindowsBase and PresentationFramework if you have not already done so.)

private void Button1_Click(object sender, EventArgs e)
{
        Window window = new Window 
        {
            Title = "My User Control Dialog",
            Content = new MyUserControl()
        };

        window.ShowDialog();
}
5
  • 20
    I also found that setting the SizeToContent = SizeToContent.WidthAndheight and ResizeMode = ResizeMode.NoResize were helpful so it lets the user control define the size. Commented Aug 11, 2009 at 18:49
  • 4
    How can we use this.Close() function to this UserControl Dialog? Commented Nov 6, 2013 at 11:18
  • 4
    private void btnClose_Click(object sender, RoutedEventArgs e) { var parent = this.Parent as Window; if (parent != null) { parent.DialogResult = true; parent.Close(); } } Commented Aug 22, 2016 at 7:08
  • 1
    If you want the dialog window to come to the top and 'flash' when other window is clicked, set the owner. Owner = Application.Current.MainWindow Commented May 30, 2019 at 14:51
  • What to do if I want to display multiple user controls simultaneously?
    – NoName
    Commented Jul 20, 2022 at 14:48
15
Window window = new Window
            {
                Title = "My User Control Dialog",
                Content = new OpenDialog(),
                SizeToContent = SizeToContent.WidthAndHeight,
                ResizeMode = ResizeMode.NoResize
            };
            window.ShowDialog();

Has worked like a magic for me. Can it be made as a modal dialog?


Ans : ShowDialog it self make it as Modal Dialog.. ...

1
  • SizeToContent is very useful. Thanks. Commented Apr 7, 2017 at 14:57
2

As far as I know you can't do that. If you want to show it in a dialog, that's perfectly fine, just create a new Window that only contains your UserControl, and call ShowDialog() after you create an instance of that Window.

EDIT: The UserControl class doesn't contain a method ShowDialog, so what you're trying to do is in fact not possible.

This, however, is:

private void Button_Click(object sender, RoutedEventArgs e){
    new ContainerWindow().ShowDialog();
}
1
namespace System.Window.Form
{
    public static class Ext
    {
        public static DialogResult ShowDialog(this UserControl @this, string title)
        {
            Window wind = new Window() { Title = title, Content = @this };
            return wind.ShowDialog();
        }
    }
}

The use of it maybe as simple as UserControlInstance.ShowDialog(). A better customized implementation would be by extending the Window class and customizing it using the the designer and code to get any functionality.

1
  • Nice Example, just in case of WPF (System.Windows.Window.ShowDialog()) Return type should be bool? (Nullable Type) Commented Aug 22, 2016 at 5:56
1

I know this is for .net 3.5, but here is a workable solution for .net 2.0

  MyUserControl myUserControl= new MyUserControl();

  Form window = new Form
  {
    Text = "My User Control",
    TopLevel = true,
    FormBorderStyle = FormBorderStyle.Fixed3D, //Disables user resizing
    MaximizeBox = false,
    MinimizeBox = false,
    ClientSize = myUserControl.Size //size the form to fit the content
  };

  window.Controls.Add(myUserControl);
  myUserControl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  window.ShowDialog();
0

You can also use MaterialDesignThemes.Wpf (downloadable on NuGet, .NET 4.5+). Then you can simply do:

{
    var view = new YourUserControl();
    var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);
}

private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{ }  //Handle Closing here
-1

If the answer by 'sixlettervariables' is modified as so, it works

private void button1_Click ( object sender, RoutedEventArgs e )                  
{
    Window window = new Window
    {
        Title = "My User Control Dialog",
        Content = new UserControl ( ),
        Height = 200,  // just added to have a smaller control (Window)
        Width = 240
    };

    window.ShowDialog ( );
}
1
  • 14
    You forgot Background = Brushes.Purple; it won't work without that. Commented May 29, 2013 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.