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

查看:932
本文介绍了如何取消组合框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. 用户选择的,删除之后的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 remember needing to do this a while back. It took me about a week of research and attempts before I found a good solution. I posted it here:

WPF:取消在数据绑定列表框,用户选择

仅供参考,它是一个基于MV-VM解决方案(如果你不使用MV-VM模式,你应该!)

FYI, it's an M-V-VM based solution (if you aren't using the M-V-VM pattern, you should be!)

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

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