I've defined a ListView
where each item is displayed with a read-only (i.e. selection-only, no TextBox
) ComboBox
(as per the ItemTemplate
).
There is a given list of possible items that can be chosen from each of these combo boxes. The caveat, however, is that no item may be selected in two combo boxes at a time. In order to ensure this, once an item has been selected in one of the combo boxes, it must be removed from all other combo boxes (obviously except for the one where it is selected), and once it gets deselected, it must be added to all other combo boxes again.
Oh, and one more thing: The order of the visible items must not change compared to the complete list.
My question is: How can I achieve this behavior?
I have tried the three possible solutions that came to my mind:
- I have written a new helper control class that takes the complete list of existing items and a list of excluded (used) items via bindings to the outside world, as well as a property for a selected item. I could include that control class in the
ItemTemplate
; the combo box within that template would then bind itsItemsSource
property to anItemsProvider
property of the helper class that was supposed to tie together the list of existing items, the list of excluded items and the selected item and return a single enumeration of items for that particular combo box.
However, I somehow got lost in all the update notifications about list changes; I fear I'd have to individually react to all combinations ofNotifyCollectionChangedAction
of the two input lists, and with the prospect of having a dozen update methods, I thought this cannot be the right way. - I changed the list of existing items to a list that stores a boolean along with each item, so I could flag each item as hidden or not. This would relieve me of the necessity to have a list of excluded items while maintaining the order of items, thereby reducing the aforementioned complexity of combined change notifications.
Unfortunately, as the list itself doesn't change with that solution, I don't know how to have the dependency property infrastructure notify myItemsSource
property in my helper class. - I don't have to use the WPF with bindings way; I can do code-behind here, too. So, I tried iterating over all
ListViewItem
s and retrieving the combo box for each of them to manually refresh the item lists. However, I couldn't find a good time to access theListViewItem
s after their item template had been loaded. There seems to be no event for that situation, andListView.ItemContainerGenerator
is read-only, so even ifItemContainerGenerator
were not a sealed class, I couldn't assign my own specializedItemContainerGenerator
that would create custom list view items where I could overrideOnApplyTemplate
.