Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Generic Types in Window.Resources

Tags:

wpf

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?

like image 862
azamsharp Avatar asked Jul 25 '26 07:07

azamsharp


1 Answers

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.

like image 128
rmoore Avatar answered Jul 28 '26 08:07

rmoore