C# 如何将自定义 UserControl 显示为对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1262115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you display a custom UserControl as a dialog?
提问by Taylor Leese
How do you display a custom UserControl
as a dialog in C#/WPF (.NET 3.5)?
如何UserControl
在 C#/WPF (.NET 3.5) 中将自定义显示为对话框?
采纳答案by user7116
Place it in a Windowand call Window.ShowDialog. (Also, add references to: PresentationCore, WindowsBase and PresentationFramework if you have not already done so.)
将它放在一个Window 中并调用Window.ShowDialog。(此外,如果您还没有添加对 PresentationCore、WindowsBase 和 PresentationFramework 的引用。)
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new MyUserControl()
};
window.ShowDialog();
}
回答by Mark Carpenter
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.
据我所知,你不能那样做。如果您想在对话框中显示它,那完全没问题,只需创建一个仅包含您的 UserControl 的新窗口,并在创建该窗口的实例后调用 ShowDialog()。
EDIT:The UserControl
class doesn't contain a method ShowDialog, so what you're trying to do is in fact not possible.
编辑:本UserControl
类不包含的方法ShowDialog的,所以你想做什么,其实是不可能的。
This, however, is:
然而,这是:
private void Button_Click(object sender, RoutedEventArgs e){
new ContainerWindow().ShowDialog();
}
回答by user474051
If the answer by 'sixlettervariables' is modified as so, it works
如果 'sixlettervariables' 的答案被修改为这样,它的工作原理
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 ( );
}
回答by WpfBegnner
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.. ...
Ans : ShowDialog 它自己使它成为模态对话框.. ...
回答by gwasterisk
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.
它的使用可能像 UserControlInstance.ShowDialog() 一样简单。更好的自定义实现是通过扩展 Window 类并使用设计器和代码对其进行自定义以获得任何功能。
回答by Darrelk
I know this is for .net 3.5, but here is a workable solution for .net 2.0
我知道这适用于 .net 3.5,但这里有一个适用于 .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();
回答by Codigo
You can also use MaterialDesignThemes.Wpf (downloadable on NuGet, .NET 4.5+). Then you can simply do:
您还可以使用 MaterialDesignThemes.Wpf(可在 NuGet、.NET 4.5+ 上下载)。然后你可以简单地做:
{
var view = new YourUserControl();
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);
}
private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{ } //Handle Closing here