有没有一种简单的方法来提示用户确认组合框选择更改,并且如果用户选择否,则不处理更改?
我们有一个组合框,更改选择会导致数据丢失。基本上,用户选择一种类型,然后他们就能够输入该类型的属性。如果它们更改了类型,我们将清除所有属性,因为它们可能不再适用。问题是,为了在选择下再次引发SelectionChanged
事件。
下面是一个代码片段:
if (e.RemovedItems.Count > 0)
{
result = MessageBox.Show("Do you wish to continue?",
"Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.No)
{
if (e.RemovedItems.Count > 0)
((ComboBox)sender).SelectedItem = e.RemovedItems[0];
else
((ComboBox)sender).SelectedItem = null;
}
}
我有两个解决方案,但我都不喜欢。
SelectionChanged
事件处理程序,更改selected项,然后再次注册SelectionChanged
事件处理程序。这意味着您必须在类中保留事件处理程序的引用,以便可以添加和删除它。ProcessSelectionChanged
布尔值作为类的一部分。始终在事件处理程序的开头检查它。在我们将选择改回之前将其设置为false,然后将其重置为true。这将会起作用,但我不喜欢使用标志来基本上使事件处理程序为空。有人对我提到的解决方案有替代方案或改进方案吗?
发布于 2012-01-13 07:06:54
我发现了这个很好的实现。
private bool handleSelection=true;
private void ComboBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
if (handleSelection)
{
MessageBoxResult result = MessageBox.Show
("Continue change?", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
{
ComboBox combo = (ComboBox)sender;
handleSelection = false;
combo.SelectedItem = e.RemovedItems[0];
return;
}
}
handleSelection = true;
}
来源:http://www.amazedsaint.com/2008/06/wpf-combo-box-cancelling-selection.html
发布于 2011-12-23 02:30:53
也许可以创建一个从ComboBox
派生的类,并覆盖OnSelectedItemChanged
(或OnSelectionChangeCommitted
)。
发布于 2012-03-07 02:58:11
在SelectionChanged
事件处理程序中进行验证允许您在选择无效时取消逻辑,但我不知道取消事件或项选择的简单方法。
我的解决方案是将WPF组合框子类化,并为SelectionChanged
事件添加一个内部处理程序。每当触发该事件时,我的私有内部处理程序都会引发一个自定义的SelectionChanging
事件。
如果在相应的SelectionChangingEventArgs
上设置了Cancel
属性,则不会引发该事件,而是将SelectedIndex
恢复为其先前的值。否则,将引发一个新的SelectionChanged
来遮蔽基本事件。希望这能有所帮助!
SelectionChanging事件的EventArgs和处理程序委托:
public class SelectionChangingEventArgs : RoutedEventArgs
{
public bool Cancel { get; set; }
}
public delegate void
SelectionChangingEventHandler(Object sender, SelectionChangingEventArgs e);
ChangingComboBox类实现:
public class ChangingComboBox : ComboBox
{
private int _index;
private int _lastIndex;
private bool _suppress;
public event SelectionChangingEventHandler SelectionChanging;
public new event SelectionChangedEventHandler SelectionChanged;
public ChangingComboBox()
{
_index = -1;
_lastIndex = 0;
_suppress = false;
base.SelectionChanged += InternalSelectionChanged;
}
private void InternalSelectionChanged(Object s, SelectionChangedEventArgs e)
{
var args = new SelectionChangingEventArgs();
OnSelectionChanging(args);
if(args.Cancel)
{
return;
}
OnSelectionChanged(e);
}
public new void OnSelectionChanged(SelectionChangedEventArgs e)
{
if (_suppress) return;
// The selection has changed, so _index must be updated
_index = SelectedIndex;
if (SelectionChanged != null)
{
SelectionChanged(this, e);
}
}
public void OnSelectionChanging(SelectionChangingEventArgs e)
{
if (_suppress) return;
// Recall the last SelectedIndex before raising SelectionChanging
_lastIndex = (_index >= 0) ? _index : SelectedIndex;
if(SelectionChanging == null) return;
// Invoke user event handler and revert to last
// selected index if user cancels the change
SelectionChanging(this, e);
if (e.Cancel)
{
_suppress = true;
SelectedIndex = _lastIndex;
_suppress = false;
}
}
}
https://stackoverflow.com/questions/8608128
复制相似问题