我有一组动态生成的单选按钮,单击它们时,会用数据填充大量文本框。它们绑定到视图模型上的一个属性,该属性根据单选按钮的标签文本从服务中提取数据。
我想要做的是在单击单选按钮时显示一个 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;
}
}
Run Code Online (Sandbox Code Playgroud)
在方法的第二行之后和返回用户选择之前,无论如何都要更新属性。
Click在RadioButton已经检查之后引发事件,但您可以改用PreviewMouseLeftButtonDownevent 并设置Handled为 true
<RadioButton ... PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"/>
Run Code Online (Sandbox Code Playgroud)
并在代码中
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;
}
}
Run Code Online (Sandbox Code Playgroud)