绑定的ObservableCollection项目用户控件在WrapPanel? [英] Binding ObservableCollection items to UserControl in WrapPanel?

查看:317
本文介绍了绑定的ObservableCollection项目用户控件在WrapPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能只是在这里失去了一些东西明显,所以我道歉,如果这是一个非常愚蠢的问题。我有我需要绑定到视图模型一个ObservableCollection景色的WrapPanel。此的ObservableCollection包含不同类型视图模型的需要在WrapPanel显示时要绑定到另一种类型的图。的目标是创建项的可包装的列表,其中每一个经由其应该被添加到WrapPanel较小视图的一个实例,显示

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.

我使用MVVM,和视图模型不必查看的直接访问。我宁愿不创建视图模型和视图,如果在所有可能的之间的绑定,所以手动添加项目到WrapPanel.Children集合不是一个可行的选择。我很茫然,以我怎么能以这样的方式,它会创建另一个视图的实例,并将它们添加到自己的孩子视图模型对象的WrapPanel集合绑定。难道我只是不正确地接近问题?我想有可能是一个DataTemplate参与其中,但它不会出现一个WrapPanel有一个DataTemplate,也不是绑定的。

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.

感谢您的任何见解。

推荐答案

您需要的是一个使用WrapPanel承载所有项目的一个ListView

What you need is a ListView that uses a WrapPanel to host all of the items.

<ListView ItemsSource={...}>
   <ListView.ItemsPanel>
     <ItemsPanelTemplate>
       <WrapPanel IsItemsHost="True" />
     </ItemsPanelTemplate>
   </ListView.ItemsPanel>
   <ListView.ItemTemplate>
      <DataTemplate>
        <!-- Fill in how you want each item to look here -->
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

这篇关于绑定的ObservableCollection项目用户控件在WrapPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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