How do you display a custom UserControl as a dialog?
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.
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.. ...
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();
}
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();
}