如何使用List< T>在xaml中? [英] how use List<T> within xaml?

查看:306
本文介绍了如何使用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.

这篇关于如何使用List&lt; T&gt;在xaml中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆