gpt4 book ai didi

c# - 在 wpf 中绑定(bind) IEnumerable> 类型对象

转载 作者:行者123 更新时间:2023-12-03 10:35:46 25 4
gpt4 key购买 nike

在我的 ViewModel 中的 WPF 应用程序(mvvm)中,我有这样的东西:

IEnumerable<IGrouping<string,SomeClass>> FeildSet = Feilds
.GroupBy(e => e.Label.Substring(0, 1));

如何在 DataTemplate 内的 View 中绑定(bind)此 FeildSet 对象?

提前致谢。

最佳答案

  • 由于FeildSet是集合(至少它是可枚举的)我建议使用内置控件来处理对象集合(即基类为 ItemsControl 的任何控件。
  • 现在,FeildSet 中的每个项目也是 SomeClass 的集合按 String 分组,所以我建议使用能够显示标题和一些内容的控件(即基类为 HeaderedContentControl 的任何控件)。
  • 每组的内容将再次显示为 ItemsControl .

  • 这为您提供了以下 Canvas :
    <ItemsControl
    ItemsSource="{Binding Path=FeildSet}">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
    <HeaderedContentControl
    Header="{Binding Path=Key}">
    <ItemsControl
    ItemsSource="{Binding Path=.}">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
    <!-- template for SomeClass here -->
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>
    </HeaderedContentControl>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>

    关于c# - 在 wpf 中绑定(bind) IEnumerable<IGrouping<string,BaseDto>> 类型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30077885/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com