Reputation: 20068
I am trying to use Generic Types in Windows.Resources section in XAML code. To attach the notification for a collection of objects my generic collection inherits from ObservableCollection as shown below:
public class PresentationModalCollection<T> : ObservableCollection<T>
{
public PresentationModalCollection(List<T> list) : base(list)
{
}
}
There is an extension method that returns a ObservableCollection for List as shown below:
public static class ExtensionMethods
{
public static PresentationModalCollection<T> ToObservableCollection<T>(this List<T> list)
{
return new PresentationModalCollection<T>(list);
}
}
Now, I want to use the PresentationModalCollection in my Window.Resources like shown below:
<Window.Resources>
<LearningWPF:PresentationModalCollection x:Key="customers">
<LearningWPF:Customer FirstName="Mohammad" LastName="Azam" />
</LearningWPF:PresentationModalCollection>
</Window.Resources>
Of course, the above code does not work. Is there any way of doing the above without having to create a class CustomerCollection which inherits from the ObservableCollection?
Upvotes: 3
Views: 896
Reputation: 15393
Mike Hillberg has some extensions that can help out with it and work pretty well. I agree that creating a CustomerCollection and collection type for each type you wanted to wrap would be overbearing. Sacha Barber also has a solution to use Generics in XAML, but his website seems to have surpassed his bandwidth limit for the moment.
Upvotes: 2