2013-08-19 98 views
15

我想在C#代碼中顯示確認框。我已經看到了上述解決方案,但它顯示我在'是'的例外,因爲'System.Nullable'不包含'是'的定義。我應該如何消除這個錯誤?C#中的確認框wpf

private void listBox1_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     if (sender is ListBoxItem) 
     { 
      ListBoxItem item = (ListBoxItem)sender; 
      Harvest_TimeSheetEntry entryToDelete = (Harvest_TimeSheetEntry)item.DataContext; 

      DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("Are you sure?", "Delete Confirmation", MessageBoxButtons.YesNo); 

      if (dialogResult == DialogResult.Yes) // error is here 
      { 
       Globals._globalController.harvestManager.deleteHarvestEntry(entryToDelete); 
      } 
      else 
      { 
       System.Windows.MessageBox.Show("Delete operation Terminated"); 
      } 

     } 
    } 

回答

76

而不是使用的WinForm的MessageBox中,使用由WPF提供的MessageBox,後來在WPF使用MessageBoxResult代替DialogResult

喜歡:

MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo); 
     if (messageBoxResult == MessageBoxResult.Yes) 
//........... 
+0

@ user1130886感謝編輯 – Habib

+0

以及如何使用它? –

+0

編輯你的例子,有不同的類型,編輯'DialogResult messageBoxResult = ...'到'MessageBoxResult messageBoxResult = ...'或者相反並且在if子句中應該是'if(messageBoxResult == MessageBoxResult.Yes)' – vinsa