如何将自定义UserControl显示为对话框?

Tay*_*ese 66 .net c# wpf

如何UserControl在C#/ WPF(.NET 3.5)中将自定义显示为对话框?

use*_*116 134

将它放在一个窗口中并调用Window.ShowDialog.

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

        window.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)

  • 我还发现设置SizeToContent = SizeToContent.WidthAndheight和ResizeMode = ResizeMode.NoResize是有帮助的,因此它允许用户控制定义大小. (11认同)
  • 我们如何在这个UserControl对话框中使用`this.Close()`函数? (3认同)
  • private void btnClose_Click(object sender, RoutedEventArgs e) { var parent = this.Parent as Window; if (parent != null) { parent.DialogResult = true; 父.关闭(); } } (3认同)

Wpf*_*ner 10

Window window = new Window
            {
                Title = "My User Control Dialog",
                Content = new OpenDialog(),
                SizeToContent = SizeToContent.WidthAndHeight,
                ResizeMode = ResizeMode.NoResize
            };
            window.ShowDialog();
Run Code Online (Sandbox Code Playgroud)

对我来说就像魔术一样.它可以作为模态对话框吗?


Ans:ShowDialog它自己将它作为模态对话.. ...