I currently have a couple different user controls that provide the same functionality: three different buttons called Select All, Deselect All, and Toggle Selected. These perform actions on a list of items that implement my ICheckable interface in each user control. I wanted to unify this stuff so that the commands and buttons would all be defined in one place only–a new user control–instead of being duplicated in two different user controls. My problem is that in one user control I’m dealing with a list of my Template class, and the other user control has a list of a Defect class. Both Template and Defect implement ICheckable, meaning the Select All, Deselect All, and Toggle Selected apply to them.
I have a generic container class SelectableItems<T> that requires T fit these constraints: where T : ICheckable, IEquatable<T>, IDeepCloneable<T>. SelectableItems<T> provides an ObservableCollection<T> Items property, along with other useful properties such as bool IsAnyItemSelected, T SelectedItem, etc. These properties would be used in implementing the Select All, etc. commands. Both Template and Defect fit all those constraints. I was going to create a dependency property in my new user control to which I would bind a SelectableItems<Template> and SelectableItems<Defect> property from my other user controls. I don’t think it’s possible to do a generic dependency property, though, because I can’t have a generic UserControl class since I’m using XAML. How should I go about this? I’m using .NET 3.5.
To sum up, this is what I want:
TemplateList user control ItemSelection user control
------------------------------------------- --------------------------
SelectableItems<Template> TemplateContainer ==Bind==> unknownType? ItemContainer
DefectList user control ItemSelection user control
--------------------------------------- --------------------------
SelectableItems<Defect> DefectContainer ==Bind==> unknownType? ItemContainer
Edit: I considered just adding dependency properties to my new ItemSelection user control for all the useful properties in the SelectableItems<T> view model, such as IsAnyItemSelected, etc. That would be fine for most of the properties, but I was hesitant to do it for ObservableCollection<T> Items because I hit the same generic problem as described above, and I didn’t trust things to work okay if I just used IEnumerable instead of ObservableCollection<something>. Maybe I should make an ObservableCollection class that isn’t generic (like in this question)?
Creating a non-generic
ObservableCollectionclass and then using a value converter to convert myObservableCollection<T>value to anObservableCollectionseems to have worked.Here are the important parts of my
ObservableCollectionclass:And here’s the value converter:
And I bind like so: