How do you display a custom UserControl
as a dialog in C#/WPF (.NET 3.5)?
views:
772answers:
4
+15
A:
Place it in a 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(this);
}
sixlettervariables
2009-08-11 18:25:10
+1 I didn't even think to do that dynamically, nice example! (better than mine :)
Pwninstein
2009-08-11 18:31:20
Good example. This is what I was looking for.
Taylor Leese
2009-08-11 18:33:46
The example could be shortened further, but that is left as an exercise for the reader ;-D
sixlettervariables
2009-08-11 18:44:36
I also found that setting the SizeToContent = SizeToContent.WidthAndheight and ResizeMode = ResizeMode.NoResize were helpful so it lets the user control define the size.
Taylor Leese
2009-08-11 18:49:11
A:
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();
}
Pwninstein
2009-08-11 18:25:35
A:
I think mvermef ment the part about the Cinch implementation of Application.DoEvents.
Zenuka
2009-08-13 06:51:19
A:
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 ( );
}