如何填充项目模板中的列表框
我刚刚学习 XAML 和 Windows Phone 7 编程。 我正在尝试为 WP7 Pivot Control 创建项目模板。我能够制作一个包含列表框的模板。是否可以在代码隐藏中访问此列表框,以便我可以根据自定义类的集合填充它?基本上它的工作原理是我有一个枢轴控件,并且该控件中的每个项目都是一个类别。对于添加的每个类别,都有一个属于该类别的项目列表。我需要能够使用该类别的项目填充每个枢轴项目上的列表。
我搜索了有关如何实现此目的的想法,并且获得了很多有关数据绑定的示例,但我不太熟悉数据绑定在 XAML 中的工作原理。
数据绑定是可行的方法还是我可以以某种方式获取对列表框的引用并自己添加项目? 任何帮助将不胜感激!
谢谢
I am just learning XAML and programming for Windows Phone 7.
Im trying to create an itemtemplate for a WP7 Pivot Control. I was able to make a template which contains a listbox. Is it possible to access this listbox in the code-behind so I can fill it based on a collection of a custom class? Basically how it works is that I have a pivot control and each item in that control is a category. For each category thatis added, there is a list of items that belong to that category. I need to be able to populate the list on each pivot item with items of that category.
I searched for ideas on how to accomplish this, and I get a lot of examples on databinding, but Im not too familiar on how databinding works in XAML.
Would databinding be the way to go or can I somehow get a reference to the listbox and add the items myself?
Any help would be greatly appriciated!
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对这个主题有一些考虑:
1)如果您通过绑定填充类别列表,那么您就没有保证完成绑定的入口点(因为绑定以延迟方式执行)。
2) 与 DataTemplate 方法相比,使用 ItemTemplate 的内容更加棘手和不可靠,您应该仅在独占情况下使用它。 LogicalTreeHelper 和 VisualTreeHelper 类将为您提供帮助。
3) 但我建议您基于 DataTemplates 构建视图,因为这是 WPF 中的常见做法。你真的认为这段代码相当复杂吗?
I've some considerations on subject:
1) If you fill Categories list via binding, then you don't have entry point where binding is guaranteed comleted (because binding executes in deferred fashion).
2) Working with ItemTemplate's content is more tricky and unreliable, than DataTemplate approach, and you should use it just in exclusive situations. LogicalTreeHelper and VisualTreeHelper classes will help you.
3) But I would recommend you to build your view based on DataTemplates as it is common practice in WPF. Do you really think that this code is pretty complicated?
如果您创建一个新的“Windows Phone Pivot 应用程序”,默认代码将显示此示例,但会在多个数据透视项中重用列表框中的相同项目。
以下概述了该示例代码的用途以及如何更改它。
在 MainPage 的构造函数中,
DataContext
设置为一个对象 (App.ViewModel)。MainPage 的此
Loaded
事件可确保填充App.ViewModel
。App.ViewModel
是MainViewModel
的实例。MainViewModel
包含一个名为“Items”的ObservableCollection
。正是它绑定到PivotItem
中的单个ListBox
:在 ListBox 中,您可以引用“Items”集合的内容。
如果您想调整它以使每个 ListBox/PivotItem 具有不同的集合,您只需调整它即可在 MainViewModel 中具有多个集合。
HTH。
IF you create a new "Windows Phone Pivot Application" the default code shows an example of this but reuses the same items in the listbox in multiple pivotitems.
Here's an overview of what that sample code is doing and how you might go about changing it.
In the constructor of MainPage, the
DataContext
is set to an object (App.ViewModel).This
Loaded
event of MainPage ensures thatApp.ViewModel
is populated.App.ViewModel
is an instance ofMainViewModel
.MainViewModel
contains anObservableCollection
called "Items". It is this that is bound to an idividualListBox
in aPivotItem
:Within the ListBox, you can refer to the contents of the "Items" collection.
If you wanted to adjust this to have different collections for each ListBox/PivotItem you could just adjust this to have multiple collections in the MainViewModel.
HTH.