简体   繁体   English

多个ComboBox的共享下拉列表(列表中的每个项目可以单独占用)

[英]Shared drop-down list for multiple ComboBoxes (each item in the list can be occupied singly)

I need to recreate a shared drop-down list selection where each item can be picked only by one control at a time.我需要重新创建一个共享下拉列表选择,其中每个项目一次只能由一个控件选择。 The closest example I can think of is player's color selection in games like Generals and Age of Empires (multiplayer).我能想到的最接近的例子是玩家在将军和帝国时代(多人)等游戏中的颜色选择。

For example, there is a list:例如,有一个列表:

{"- None -", "Blue", "Red", "Yellow", etc.} {“-无-”、“蓝色”、“红色”、“黄色”等}

There are four ComboBoxes.有四个组合框。 Each of them starts as "- None -".他们每个人都以“-无-”开头。 If ComboBoxA takes "Blue", no other ComboBox should have "Blue" in their list.如果 ComboBoxA 采用“蓝色”,则其他 ComboBox 的列表中不应包含“蓝色”。 Though ComboBoxA still should have "Blue" in its List.虽然 ComboBoxA 的列表中仍然应该有“蓝色”。 If ComboBoxA picks another option, all other ComboBoxes should now have "Blue" again.如果 ComboBoxA 选择另一个选项,所有其他 ComboBoxes 现在应该再次具有“蓝色”。 The same with Red, Yellow, etc.与红色、黄色等相同。

Any ideas on how to achieve this?关于如何实现这一目标的任何想法? MVVM is preferable but not a must. MVVM 是可取的,但不是必须的。

You could create a Class lets say: Colors.您可以创建一个 Class 让我们说:Colors。 That class has a minimum of 2 properties "Name" for the color and a boolean "Selected" if that color is selected.如果选择了该颜色,则 class 至少有 2 个属性“名称”用于颜色和 boolean“已选择”。 After that you create a List of Colors Class.之后,您创建一个列表 Colors Class。 You put a Method in that List Class: GetColors(exeptColor).您在该列表中放置了一个方法 Class: GetColors(exeptColor)。 The exeptColor is the actual selected value of the Combobox. exeptColor 是 Combobox 的实际选择值。 The GetColors method returns a list of only the Colors that are not selected. GetColors 方法仅返回未选择的 Colors 的列表。

Based on answers on my other question, partially the answer given by Herman and a lot of customization, I came to using a multiValue converter.根据我的另一个问题的答案,部分是 Herman 给出的答案和大量定制,我开始使用多值转换器。 Its convert method is as follows:其转换方法如下:

ObservableCollection<WorksheetColumn> worksheets = new ObservableCollection<WorksheetColumn>(values[0] as IEnumerable<WorksheetColumn>);
ComboBox combo = values[1] as ComboBox;
WorksheetColumn selected = combo.SelectedItem as WorksheetColumn;
int keepColumn = selected.ID;
return worksheets.Where(header => header.ID == 0 || header.Selected == false || header.ID == keepColumn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM