how use List<T> within xaml?
-
08-01-2021 - |
Question
So I am pretty sure that up in the definition part I need to include something along the lines of:
xmlns:s="clr-namespace:System.Collections.Generic;assembly=?????"
but I just do not know what to put in place of the ???'s.
What I want to do with the code is this:
<UserControl.DataContext>
<ObjectDataProvider
MethodName="CreateNodes"
ObjectType="{x:Type local:TreeViewModel}" >
<ObjectDataProvider.MethodParameters>
<s:List<T>>
{Binding Nodes}
</s:List<T>>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.DataContext>
So that when I make the objectDataProvider call, I can pass the list in to the method that it is calling (CreateNodes)...
How do I go about doing this?
thanks!
Edit - could be a fix?
I just put this in the method, instead of passing in the list, it is just an app variable...I dont know if app variables are bad though
List<TNode> existingNodes;
if (Application.Current.Properties.Contains("ExistingNodes")) existingNodes = Application.Current.Properties["ExistingNodes"] as List<TNode>;
else existingNodes = new List<TNode>();
Solution
The assembly
part of the XML namespace declaration would be mscorlib
.
But anyway, XAML doesn't support generics (*), so you can't do it. Instead, you could create a class that inherits List<T>
and use it in XAML:
class ListOfFoo : List<Foo>
{
}
(1) Actually generics are supported in XAML 2009, but most of XAML 2009 is not supported in compiled XAML. See this question for more information.