多值ListBox自动选择不需要的项目

多值ListBox自动选择不需要的项目

本文介绍了多值ListBox自动选择不需要的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,该对象具有从所选列表中选择的一些属性-假设一个促销活动可以具有0到X个通信渠道.为了显示/编辑此信息,我使用了一个带有选项SelectionMode == MultiExtended的列表框.

I have an object that has some attributes from the list selected - let's say a Promotion that can have 0 to X communication channels. To display/edit this information I am using a listbox with option SelectionMode==MultiExtended.

但是在某些情况下,它表现得很奇怪

But in some cases it is behaving strangely

  1. 我选择了促销"并选择了2个沟通渠道(三个渠道中的第一个和最后一个)

  1. I have Promotion with 2 communication channels selected (first and last out of three channels),

我单击第二个通道(以前是唯一未选择的通道),并且知道它显示了第一个和第二个通道已被选中(我在列表框SelectedIndexChanged事件的开头放置了一个复选框-它显示了(虽然我单击了不按住Ctrl或Shift键的单个项目),但在这种情况下,SelectedIndexChanged事件被触发了两次,在所有其他情况下,它都仅触发了一次

I click on a second channel (that previously was the only unselected channel) and know it shows, that 1st and 2nd channels are selected (I placed a check at the beginning of the listbox SelectedIndexChanged event - and it shows that SelectedItems.Count==2, although I clicked on a single item not holding Ctrl or Shift keys) and in this case SelectedIndexChanged event is triggered twice in all other cases it is triggered just once

只有在我第一次打开此对话框窗体时,如果我手动选择频道"的第一项和第三项,然后单击第二项,然后它才能正常工作,这种情况才会发生

This happens only after the first time I open this dialogform, if I manually select 1st and 3rd item of Channels, and then click on the 2nd item - then it works properly

正在处理的问题的屏幕录像

http://screencast.com/t/lVs0e9oau

foreach (var ct in Promotion_operations.Configuration.PromoCommunicationTypes)
{
    KeyValuePair<string, PromotionCommunicationType> nct =
        new KeyValuePair<string, PromotionCommunicationType>(ct.Name, ct);
    communications.Add(nct);
}
PromotionCommunicationList.DataSource = communications; //Promotion_operations.Configuration.PromoCommunicationTypes;
PromotionCommunicationList.DisplayMember = "Key";
PromotionCommunicationList.ValueMember = "Value";

这是我根据促销数据加载所选项目的方式

private void LoadSelectedCommunicationsList(ListBox lstbox, List<PromotionCommunication> communications)
{
    lstbox.SelectedItems.Clear();
    foreach (var ct in communications)
    {
        for (int j = 0; j < lstbox.Items.Count; j++)
        {
            if (ct.CommunicationType.Id == ((KeyValuePair<string, PromotionCommunicationType>)lstbox.Items[j]).Value.Id)
            {
                lstbox.SelectedItems.Add(lstbox.Items[j]);
            }
        }
    }
}

这种行为可能是什么原因?

单击一个先前未选择的列表会同时选择-新选择的项目和列表的第一项吗?

What could be the cause of this behaviour?

that clicking on one previously unselected list selects both - newly selected item and first item of the list?

推荐答案

您的PromotionCommunicationListHistoryCommunicationListDataSource共享对对象列表的相同引用.也就是说,它们具有相同的BindingContext并共享相同的CurrencyManager. CurrencyManager会记住ListBox控件中的选定项目,这就是您创建冲突的地方,因为他保存了两个ListBoxes的选定项目.因为将不同" 列表(原始副本的副本)设置为DataSource时创建了新的CurrencyManager,所以您已经找到解决问题的方法.另一种可能的解决方案是为您的ListBox控件之一创建新的BindingContext.
您可以尝试以下方法:

Your PromotionCommunicationList and HistoryCommunicationList are sharing the same reference to your list of objects as DataSource. That said, they have the same BindingContext and share the same CurrencyManager. CurrencyManager is remembering selected items of your ListBox control and that's where your conflict is created because he's saving selected items of both of your ListBoxes. You already found the solution for your problem because new CurrencyManager is created when you set "different" list (the copy of your original one) as DataSource. Another possible solution would be the creation of new BindingContext for one of your ListBox controls.
You can try this out:

PromotionCommunicationList.DataSource = communications;
(..)
HistoryCommunicationList.BindingContext = new BindingContext(); // Add this
HistoryCommunicationList.DataSource = communications;

它应该可以解决您的问题.有关BindingContext的更多信息,请单击链接MSDN.

It should solve your problem. For more information about BindingContext check this link on MSDN.

这篇关于多值ListBox自动选择不需要的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:41