如何使用List在 xaml 中?
所以我很确定在定义部分我需要包含以下内容:
xmlns:s="clr-namespace:System.Collections.Generic;assembly=?????"
但我只是不知道用什么来代替 ??? 。
我想要对代码执行的操作是这样的:
<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)...
我该如何执行此操作?
谢谢!
编辑 - 可以修复吗?
我只是将其放入方法中,而不是传入列表,它只是一个应用程序变量...我不知道应用程序变量是否不好
List<TNode> existingNodes;
if (Application.Current.Properties.Contains("ExistingNodes")) existingNodes = Application.Current.Properties["ExistingNodes"] as List<TNode>;
else existingNodes = new List<TNode>();
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>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XML 命名空间声明的
程序集
部分将是mscorlib
。但无论如何,XAML 不支持泛型(*),所以你不能这样做。相反,您可以创建一个继承
List
的类并在 XAML 中使用它:(1) 实际上,XAML 2009 中支持泛型,但大多数已编译的 XAML 不支持 XAML 2009。有关详细信息,请参阅此问题。
The
assembly
part of the XML namespace declaration would bemscorlib
.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:(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 属性:
那么您可以在 XAML 中使用它:
In my case, this is ok.
If you define List property like this:
Then you use it in XAML: