0

我想将以下方法从 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;
    }
4

3 回答 3

1

您必须自己实现逻辑来确认和关闭您的消息框。

创建公共属性成功

Public bool Success {get;set;}

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

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

然后你可以检查属性成功

可能是我错过了一些语法,但我希望你能得到它要去的地方:)

于 2013-06-20T08:31:39.053 回答
1

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

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

于 2013-06-20T08:50:23.997 回答
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;
    }
于 2014-02-18T08:33:50.267 回答