我有一个列表类型的组合框。我通过数据上下文绑定(bind)了 ItemsSource 和 ItemSelected。如果所选项目已更改,则我会显示一条确认用户操作的弹出消息。单击“确定”后,选择会更改。但是单击取消时,应取消选择并保留上一个项目。下面是绑定(bind)到组合框的 SelectedItem 的属性。
Public SomeClass Sel
{
get
{
return _sel;
}
set
{
if (_sel != value)
{
var sview = _sel;
if (Compare())
{
_sel = value;
if (Sel != null)
IsDefault = Sel.IsDefault;
OnPropertyChanged(() => Sel);
}
else
{
MessageBoxResult result = MessageBox.Show("Message.", "Owb Message", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
_sel = value;
if (Sel != null)
IsDefault = Sel.IsDefault;
OnPropertyChanged(() => Sel);
}
else
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
_sel = sview;
OnPropertyChanged("Sel");
}), DispatcherPriority.Send, null);
return;
}
}
}
}
}
组合框位于弹出窗口中。那么 Dispatcher 对象在这种情况下会起作用吗?
最佳答案
我猜选择的值被保留,但 View 没有正确更新。
看看这篇文章:http://www.codeproject.com/Articles/407550/The-Perils-of-Canceling-WPF-ComboBox-Selection .基本上,.Net 3.5 中确实存在的少数解决方法在 .Net 4.0 中不再适用。
关于wpf - 使用 MVVM 在 wpf 的组合框中取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14122043/