0

I want to pass a type parameter to my *.xaml.cs file. The C# code will look like this:

public partial class Filter<T> : Window where T : IFilterableType
{
    private readonly IEnumerable<T> _rows;
    public Filter(IEnumerable<T> rows)
    {
        this._rows = rows;
    }
}

Since this is a partial class, and since Visual Studio generates the other part of the class, I am concerned that my type parameter <T> will get deleted when Visual Studio regenerates the other part of the partial class. So far in my testing, this hasn't happened, but I want to be sure.

Can I pass a type argument to a *.xaml.cs file like this?

If not, is there any other way for my *.xaml.cs class to have a private List of some generic type? I would try something like what's below, but that of course doesn't compile.

public partial class Filter : Window
{
    private IEnumerable<T> _rows;
    public Filter() { }

    public void LoadList(IEnumerable<T> rows) where T : IFilterableType
    {
        this._rows = rows;
    }
}
user2023861
  • 8,030
  • 9
  • 57
  • 86
  • 1
    It won't get deleted, but you can't use generic types with [compiled XAML used by WPF](http://msdn.microsoft.com/en-us/library/ee792007.aspx) (currently). – user7116 Apr 23 '13 at 19:30

2 Answers2

0

Unfortunately neither of your requested options are possible in XAML

Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • I don't understand this answer. Maybe I am misunderstanding something. His question, to me, seems to be about code behind and not the XAML itself. – Nick Freeman Apr 23 '13 at 18:54
  • Is my first option impossible because Visual Studio will remove the ``? Is there any way for an xaml.cs file to have a generic list like this? – user2023861 Apr 23 '13 at 19:02
  • As I said, a window cannot have a generic type argument. The only thing you can do is make a generic method, but you cannot create a generic property since you need to get the generic parameter from somewhere and since it's not possible on the class there's no way to do this. – Kenneth Apr 23 '13 at 20:45
  • Actually you didn't say that. Do you have documentation for this? I can't find any documentation concerning this issue on MSDN. – user2023861 Apr 24 '13 at 13:41
  • I have tried the same thing before but it just doesn't work. Here's a thread on MSDN asking the same thing: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/02ca0499-80af-4c56-bb80-f1185a619a9e – Kenneth Apr 24 '13 at 15:44
0

Here's another option. I've gotten this to work, but it sure is ugly code. I use a simple object variable to hold the generic list. I use methods with constrained type parameters to ensure that I'm using the IFilterableType interface. I also check types in my DisplayList method to be sure I'm using the correct implementation of IFilterableType.

If I call this.DisplayList using FilterB instead of FilterA, I will get an exception. This is the best solution I can come up with.

public partial class Filter : Window
{
    public Filter()
    {
        List<FilterA> listA = new List<FilterA>();
        this.SetList<FilterA>(listA);
        this.DisplayList<FilterA>();
    }

    public interface IFilterableType { string Name { get; } }
    public class FilterA : IFilterableType { public string Name { get { return "A"; } } }
    public class FilterB : IFilterableType { public string Name { get { return "B"; } } }


    private object _myList;
    private Type _type;

    public void SetList<T>(List<T> list) where T : IFilterableType
    {
        this._myList = list;
        this._type = typeof(T);
    }

    public void DisplayList<T>() where T : IFilterableType
    {
        if (this._myList is List<T>)
            this.DataContext = (List<T>)this._myList;
        else
            throw new ArgumentException();
    }
}
user2023861
  • 8,030
  • 9
  • 57
  • 86