2013-06-20 61 views
0

需要將以下梅索德從WinForm的轉換爲WPF我如何的DialogResult 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; 
    } 
+0

什麼都不行? – JMan

+0

什麼不起作用?你能清楚地解釋一下這個問題嗎? –

+0

什麼不起作用的是,第一個代碼是一個WinForm代碼,我想將它轉換爲WPF – tp1

回答

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

不知道我理解的問題,但在WPF Window.DialogResultbool?這意味着它可以是true,falsenull,這取決於結果是什麼。如果您想關閉WPF Window成功,您需要將DialogResult設置爲true。當您有Button.IsDefault = "true"時,它將觸發ENTER上的點擊事件,但在您設置了DialogResult之前不會關閉對話框。

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

和你DialogResultWindow.ShowDialog()

if (myDlg.ShowDialog() == true) .... 

結果被傳遞。當你有Button.IsCancel = "true"然後,在ESC,它會觸發點擊事件,並自動關閉對話框,DialogResult=False

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; 
    } 
+0

你能解釋一下如何解決這個問題。也許只顯示代碼將無助於OP正在發生什麼。 –