How do you show a custom UserControl as a dialog?

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

+66
c # wpf
Aug 11 '09 at 18:23
source share
6 answers

Put it in Window and call Window.ShowDialog .

 private void Button1_Click(object sender, EventArgs e) { Window window = new Window { Title = "My User Control Dialog", Content = new MyUserControl() }; window.ShowDialog(); } 
+133
Aug 11 '09 at 18:25
source share
 Window window = new Window { Title = "My User Control Dialog", Content = new OpenDialog(), SizeToContent = SizeToContent.WidthAndHeight, ResizeMode = ResizeMode.NoResize }; window.ShowDialog(); 

It worked like magic for me. Can this be done as a modal dialogue?




Ans: ShowDialog he does it himself as a modal dialogue .....

+10
Aug 30 '13 at 8:08
source share

As far as I know, you cannot do this. If you want to show it in a dialog box, that’s fine, just create a new window that contains only your UserControl, and call ShowDialog () after instantiating this window.

EDIT: The UserControl class does not contain the ShowDialog method, so what you are trying to do is actually not possible.

This, however, is as follows:

 private void Button_Click(object sender, RoutedEventArgs e){ new ContainerWindow().ShowDialog(); } 
+1
Aug 11 '09 at 18:25
source share
 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(); } } } 

Using this is possible simply as UserControlInstance.ShowDialog (). The best individual implementation would be to extend the Window class and configure it using the constructor and code to get any functions.

+1
Apr 6 '15 at 6:17
source share

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
Jun 21 '19 at 7:40
source share

If the answer to "sixlettervariables" changes as such, 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 ( ); } 
-one
Oct 13 '10 at 4:14
source share



All Articles