将 ObservableCollection 项绑定到 WrapPanel 中的 UserControl?
我可能只是在这里遗漏了一些明显的东西,所以如果这是一个非常愚蠢的问题,我深表歉意。我的视图中有一个 WrapPanel,我需要将其绑定到 ViewModel 上的 ObservableCollection。此 ObservableCollection 包含不同类型的 ViewModel,在 WrapPanel 中显示时需要将其绑定到另一种类型的视图。目标是创建一个可包装的项目列表,每个项目都通过应添加到 WrapPanel 的较小视图的实例显示。
我使用的是 MVVM,ViewModel 无法直接访问 View。如果可能的话,我宁愿不在 ViewModel 和 View 之间创建绑定,因此手动将项目添加到 WrapPanel.Children 集合不是一个可行的选择。我不知道如何将子 ViewModel 对象的集合绑定到 WrapPanel,从而创建另一个视图的实例并将它们添加到自身。我是否只是错误地处理了这个问题?我认为可能涉及到一个 DataTemplate,但 WrapPanel 似乎没有 DataTemplate,也不是可绑定的。
感谢您的任何见解。
I may just be missing something obvious here, so I apologize if this is a really dumb question. I have a WrapPanel in a view that I need to bind to an ObservableCollection on the ViewModel. This ObservableCollection contains a different type of ViewModel that needs to be bound to another type of view when displayed in the WrapPanel. The goal is to create a wrappable list of items, each of which displays via an instance of a smaller view which should be added to the WrapPanel.
I am using MVVM, and the ViewModel does not have direct access to the View. I would rather not create a binding between the ViewModel and the View if at all possible, so manually adding items to the WrapPanel.Children collection is not a viable option. I am at a loss as to how I can bind a collection of child ViewModel objects to the WrapPanel in such a way that it will create instances of another view and add them to itself. Am I simply approaching the problem incorrectly? I figure there is probably a DataTemplate involved, but it doesn't appear that a WrapPanel has a DataTemplate, nor is it bindable.
Thanks for any insight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的是一个使用 WrapPanel 来托管所有项目的 ListView。
What you need is a ListView that uses a WrapPanel to host all of the items.
使用 ItemsControl,并将其 ItemsPanel 设置为 WrapPanel:
Use an ItemsControl, and set its ItemsPanel to a WrapPanel: