2016-03-22 39 views
1

我有一組動態生成的單選按鈕,單擊時,用數據填充大量文本框。它們綁定到視圖模型上的一個屬性,該屬性根據單選按鈕的標籤文本從服務中提取數據。WPF - 停止單選按鈕單擊消息框

我想要做的是當單擊單選按鈕時顯示一個MessageBox,所以如果用戶不小心(或故意)點擊另一個單選按鈕,我可以確認這是他們想要做的。

我可以捕獲單擊事件並顯示一個MessageBox,但底層屬性無論如何都會改變,從而觸發數據更改。有沒有一種方法可以在MessageBox顯示時停止執行? Click事件是否使用了錯誤的事件?我對WPF很新穎。

單選按鈕單擊事件:

private void RadioButton_Click(object sender, RoutedEventArgs e) 
{ 
    var radioButton = sender as RadioButton; 
    MessageBoxResult result = MessageBox.Show("Choosing this sample will override any changes you've made. Continue?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); 
    if (result == MessageBoxResult.Yes) 
    { 
    radioButton.IsChecked = true; 
    return; 
    } 
} 

方法的第二行後和用戶的選擇之前返回時的屬性仍會更新。

+0

你是什麼_underlying財產的意思是改變無論如何,觸發數據change_?我不明白在顯示消息框時觸發了什麼。 – dkozl

+0

加載數據並同時顯示消息框,但不給用戶時間回覆 – JB06

+0

您的意思是在出現消息框之前勾選了「RadioButton」? – dkozl

回答

2

Click事件RadioButton後引發已被選中,但你可以使用PreviewMouseLeftButtonDown事件,而不是和設置Handled爲true

<RadioButton ... PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"/> 

,並在代碼

private void RadioButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    e.Handled = true; 
    var radioButton = sender as RadioButton; 
    MessageBoxResult result = MessageBox.Show("Choosing this sample will override any changes you've made. Continue?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); 
    if (result == MessageBoxResult.Yes) 
    { 
     radioButton.IsChecked = true; 
    } 
} 
+0

修復它。不知道預覽事件,謝謝! – JB06

+0

瞭解有關路由事件的更多信息並檢查[this](https://msdn.microsoft.com/library/ms742806(v = vs.100).aspx#how_event_processing_works)鏈接以查看路由事件的順序(和方向)被執行 – dkozl