简单绑定到WPF中的列表 [英] Simple binding to a list in WPF

查看:90
本文介绍了简单绑定到WPF中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习WPF数据绑定的绳索.以下简单绑定有什么问题?
我有一个带有用户控件的某些实例的窗口:

Learning the ropes of WPF data binding.. What''s wrong with the following simple binding?
I have a window with some instances of a user control:

<usercontrol mode="hold" x:class="MyUserCtrl" x:name="CommandRow" xmlns:x="#unknown" /><usercontrol.resources>
  <datatemplate x:key="MyItemListTemplate" xmlns:x="#unknown">
     <stackpanel orientation="Horizontal">
       <Label Content="{Binding Path=ItemName}" />                
       <textbox text="{Binding Path=ItemValue}" />
     </stackpanel>
   </datatemplate>
</usercontrol.resources>
<listbox name="ListBoxParams">
                ItemsSource="{Binding ElementName=CommandRow, Path=_MyItems}"
                ItemTemplate="{StaticResource MyItemListTemplate}" />
</listbox>


并在用户控件后面的代码中具有


and in the code behind the user control has

List<MyItem> _MyItems

推荐答案



您需要正确设置数据上下文-不幸的是,您的代码示例有些乱码,是否有可能用更多的代码来更新问题?

我们需要查看CommandRow对象是什么.

本质上,您应该有一个包含命令的viewmodel类,CommandRow应该与之绑定,列表框参数也应与之绑定-您应避免将一个UI元素列表源绑定到另一个UI列表源,看起来它不应在这种情况下需要.

我可以建议我有关WPF入门的文章吗?


[ WPF入门^ ]

这详细解释了绑定到列表的方法-如果您必须按照看起来的方式进行操作,那么我们将需要更好的代码示例!

让我知道我是否可以做些其他事情来帮助您.
Hi,

You need to have a datacontext set properly - unfortunately your code examples have become a little bit garbled, is there any chance you can update the question with a bit more code?

We need to see what the CommandRow object is.

In essence you should have a viewmodel class that contains your commands, the CommandRow should bind to that and so should the list box params - you should try and avoid binding one UI element list source to another, it looks like it shouldn''t be needed in this case.

Can I suggest my article on getting started with WPF?


[Getting Started with WPF^]

This explains binding to a list in detail - if you do have to do it the way it appears you are doing it, we''re going to need better code samples!

Let me know if I can do anything else to help.


这篇关于简单绑定到WPF中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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