如何使用List< T>在xaml中?
因此,我非常确定在定义部分中需要包括以下内容:
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>
这样,当我进行objectDataProvider调用时,可以将列表传递给它正在调用的方法(CreateNodes)...
So that when I make the objectDataProvider call, I can pass the list in to the method that it is calling (CreateNodes)...
我该怎么做?
谢谢!
编辑-可以解决吗?
我只是将其放在方法中,而不是传递列表,它只是一个应用程序变量...我不知道应用程序变量是否不好
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>();
XML名称空间声明的assembly
部分将是mscorlib
.
The assembly
part of the XML namespace declaration would be mscorlib
.
但是无论如何,XAML不支持泛型(*),所以您不能这样做.相反,您可以创建一个继承List<T>
的类并在XAML中使用它:
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)XAML 2009中实际上支持泛型 ,但是编译后的XAML中不支持大多数XAML 2009 .有关更多信息,请参见此问题.
(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.