如何将DialogResult表单winform转换为WPF

时间:2013-06-20 08:12:03

标签: c# wpf

我想将以下方法从winform转换为WPF

private void btnOK_Click(object sender, System.EventArgs e)
{
    if (!EnterNewSettings())
        DialogResult = DialogResult.None;
}

我已经这样做了,但是没有用。

private void btnOK_Click(object sender, System.EventArgs e)
 { 
if (!EnterNewSettings())
 MessageBoxResult result = MessageBoxResult.None;
    }

3 个答案:

答案 0 :(得分:1)

您必须实施逻辑以确认并关闭自己的消息框。

创建公共财产成功

Public bool Success {get;set;}

如果您可以关闭表单并使其成功,请在ok按钮中使用此实现:

private void btnOK_Click(object sender, System.EventArgs e)
{ 
   if (!EnterNewSettings()){
   MessageBoxResult result = MessageBoxResult.None;
   }else{
      Success = true;
      Close();
   }
}

然后你可以查看物业成功

可能是我缺少一些语法,但我希望你能得到它的位置:)

答案 1 :(得分:1)

我不确定我是否理解此问题,但在WPF中Window.DialogResultbool?,这意味着它可以是truefalsenull,具体取决于是结果。如果您想成功关闭WPF Window,则需要将DialogResult设置为true。当您拥有Button.IsDefault = "true"时,它会触发ENTER上的点击事件,但在设置DialogResult之前不会关闭该对话框。

private void btnOK_Click(object sender, System.EventArgs e)
{ 
   if (EnterNewSettings()) DialogResult = true;
}

并且DialogResult

会传递您的Window.ShowDialog()
if (myDlg.ShowDialog() == true) ....

如果您有Button.IsCancel = "true",那么在ESC上,它会触发点击事件并自动关闭与DialogResult=False的对话

答案 2 :(得分:0)

    private MessageBoxResult isBlaBlaa()
    {
        Window w = new Window();
        w.Tag = MessageBoxResult.Cancel;

        Grid grid = new Grid();
        grid.Margin = new Thickness(30);

        grid.Children.Add(new TextBlock()
        {
            Text = "Bla blaa",
            Margin = new Thickness(0, 0, 0, 20)
        });

        Button btn;
        btn = new Button()
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Content = "Cancel",
            Width = 100,
            Height = 30,

        };
        btn.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) => { w.Tag = MessageBoxResult.Cancel; w.DialogResult = false; });
        grid.Children.Add(btn);

        btn = new Button()
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
            Content = "No",
            Width = 100,
            Height = 30,

        };
        btn.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) => { w.Tag = MessageBoxResult.No; w.DialogResult = false; });
        grid.Children.Add(btn);

        btn = new Button()
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Right,
            Content = "Yes",
            Width = 100,
            Height = 30,

        };
        btn.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) => { w.Tag = MessageBoxResult.Yes; w.DialogResult = true; });
        grid.Children.Add(btn);


        w.Content = grid;

        w.ShowDialog();

        return (MessageBoxResult)w.Tag;
    }