如何取消 ComboBox SelectionChanged 事件? [英] How to cancel a ComboBox SelectionChanged event?

查看:23
本文介绍了如何取消 ComboBox SelectionChanged 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以提示用户确认组合框选择更改,并且如果用户选择否"则不处理更改?

Is there an easy method to prompt the user to confirm a combo box selection change and not process the change if the user selected no?

我们有一个组合框,更改选择会导致数据丢失.基本上,用户选择一种类型,然后他们就可以输入该类型的属性.如果他们更改类型,我们将清除所有属性,因为它们可能不再适用.问题是,在选择下,您再次引发 SelectionChanged 事件.

We have a combo box where changing the selection will cause loss of data. Basically the user selects a type, then they are able to enter attributes of that type. If they change the type we clear all of the attributes as they may no longer apply. The problem is that to under the selection you raise the SelectionChanged event again.

这是一个片段:

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;
    }
}

我有两种解决方案,但我都不喜欢.

I have two solutions, neither of which I like.

  1. 在用户选择'No'后,移除SelectionChanged事件处理程序,改变选中的项目,然后注册SelectionChanged再次事件处理程序.这意味着您必须保留类中事件处理程序的引用,以便您可以添加和删除它.

  1. After the user selects 'No', remove the SelectionChanged event handler, change the selected item and then register the SelectionChanged event handler again. This means you have to hold onto a reference of the event handler in the class so that you can add and remove it.

创建一个 ProcessSelectionChanged 布尔值作为类的一部分.始终在事件处理程序开始时检查它.在我们更改选择之前将其设置为 false,然后将其重置为 true.这会起作用,但我不喜欢使用标志来基本上使事件处理程序无效.

Create a ProcessSelectionChanged boolean as part of the class. Always check it at the start of the event handler. Set it to false before we change the selection back and then reset it to true afterwards. This will work, but I don't like using flags to basically nullify an event handler.

有人对我提到的解决方案有替代解决方案或改进方案吗?

Anyone have an alternative solution or an improvement on the ones I mention?

推荐答案

我发现了这个很好的实现.

I found this good implementation.

 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

这篇关于如何取消 ComboBox SelectionChanged 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆