将 ObservableCollection 项绑定到 WrapPanel 中的 UserControl?

发布于 2024-08-07 11:01:06 字数 509 浏览 1 评论 0原文

我可能只是在这里遗漏了一些明显的东西,所以如果这是一个非常愚蠢的问题,我深表歉意。我的视图中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月苍老的讽刺 2024-08-14 11:01:06

您需要的是一个使用 WrapPanel 来托管所有项目的 ListView。

<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>

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>
宁愿没拥抱 2024-08-14 11:01:06

使用 ItemsControl,并将其 ItemsPanel 设置为 WrapPanel:

<ItemsControl ItemsSource="{Binding Something}" ItemTemplate="{StaticResource YourDataTemplate}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

Use an ItemsControl, and set its ItemsPanel to a WrapPanel:

<ItemsControl ItemsSource="{Binding Something}" ItemTemplate="{StaticResource YourDataTemplate}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文