多个组合框绑定到公共源,强制执行不同的选择 [英] Multiple ComboBoxes bound to a common source, enforcing distinct selections

查看:134
本文介绍了多个组合框绑定到公共源,强制执行不同的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将多个ComboBox绑定到一个公共源集合,并强制执行一个ComboBox选择,该选定的项目被从其他ComboBox的一个可能性中删除。该集合是动态构建的,所以我在代码中做。

I'm trying to bind multiple ComboBox's to a common source collection and enforce that once a ComboBox selection is made, that selected item is removed from the other ComboBox's as a possibility. The collection is built dynamically, so I'm doing it in code.

我已经尝试以几种方式实现这个到目前为止,我只是不能来

I have tried implementing this in several ways so far, and I just can't seem to come up with something that really works.

我试过使用默认视图的Filter谓词,但它只传递项目,我无法知道哪个控件是做过滤器(它甚至概念上没有意义)。

I've tried using the default view's Filter predicate, but it only passes the item, and I have no way of knowing which control is doing the filter (and it doesn't even make sense conceptually).

我试过创建新的CollectionView,但行为最终是不同的(获取SelectionChange事件我之前没有使用默认视图)。

I've tried creating new CollectionView's, but the behavior ends up being different (getting SelectionChange events where I didn't before with the default view).

我一直在敲我的头几个小时,它似乎不想工作。我将感谢有更多经验的WPF帮助我出一个工作榜样。我真的很喜欢它不从集合中自动选择项目,并开始空白(否则,每个ComboBox将有一个不同的自动选择,太过于夸张)。

I've been banging my head against this for hours and it just does not seem to want to work. I would appreciate someone more experienced with WPF helping me out with a working example. I would really like it to not auto-select items from the collection and start blank (otherwise, each ComboBox will have a distinct auto-selection which is far too presumptuous).

感谢

推荐答案

我不确定我第一次做错了什么,但我改变了几件事情,得到了我的原始尝试工作。

I'm not exactly sure what I did wrong the first time around, however I did change a few things around and got my original attempt working.

这可能是我遇到的问题是直接使用CollectionView的结果,这在调试输出中有一个警告。它说,直接使用CollectionView不完全支持,可能有错误的行为。

It's possible the problems I was running into were the result of using CollectionView directly, which there is a warning about in the debug output. It states that using CollectionView directly is not fully supported and may have buggy behavior.

我最终切换到ListCollectionView。我还创建了一个新的List<>包含我的源集合的内容,这允许我添加一个空白对象到列表(而不改变我的原始数据结构)。我只是忽略它的选择更改,并无条件包括在过滤器。

I ended up switching to a ListCollectionView. I also created a new List<> containing the contents of my source collection, which allowed me to add a blank object to the list (without changing my original data structure). I simply ignore it on selection change and include it unconditionally in the Filter.

所以使其工作的步骤:

1)ListCollectionView与源IList

2)向视图中添加过滤谓词

3)使用视图为源创建绑定

4)ComboBox.ItemsSource上的combo.SetBinding

5)在选择更改事件时,确保将每个组合的ItemsSource转换为ICollectionView并调用Refresh(因为它需要重新过滤,并且不会注意到属性更改通知)

So the steps to making it work:
1) ListCollectionView with source IList
2) Add Filter predicate to the view
3) Create Binding with view as Source
4) combo.SetBinding on ComboBox.ItemsSource
5) On selection changed event, make sure to cast each combo's ItemsSource to an ICollectionView and call Refresh (since it will need to refilter, and property change notifications aren't noticed)

这篇关于多个组合框绑定到公共源,强制执行不同的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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