• <small id='wUsMz'></small><noframes id='wUsMz'>

        <bdo id='wUsMz'></bdo><ul id='wUsMz'></ul>

      <i id='wUsMz'><tr id='wUsMz'><dt id='wUsMz'><q id='wUsMz'><span id='wUsMz'><b id='wUsMz'><form id='wUsMz'><ins id='wUsMz'></ins><ul id='wUsMz'></ul><sub id='wUsMz'></sub></form><legend id='wUsMz'></legend><bdo id='wUsMz'><pre id='wUsMz'><center id='wUsMz'></center></pre></bdo></b><th id='wUsMz'></th></span></q></dt></tr></i><div id='wUsMz'><tfoot id='wUsMz'></tfoot><dl id='wUsMz'><fieldset id='wUsMz'></fieldset></dl></div>
      <legend id='wUsMz'><style id='wUsMz'><dir id='wUsMz'><q id='wUsMz'></q></dir></style></legend>
      1. <tfoot id='wUsMz'></tfoot>
      2. WPF:将集合与集合绑定到具有组的列表框

        时间:2023-10-07
        <i id='X78qL'><tr id='X78qL'><dt id='X78qL'><q id='X78qL'><span id='X78qL'><b id='X78qL'><form id='X78qL'><ins id='X78qL'></ins><ul id='X78qL'></ul><sub id='X78qL'></sub></form><legend id='X78qL'></legend><bdo id='X78qL'><pre id='X78qL'><center id='X78qL'></center></pre></bdo></b><th id='X78qL'></th></span></q></dt></tr></i><div id='X78qL'><tfoot id='X78qL'></tfoot><dl id='X78qL'><fieldset id='X78qL'></fieldset></dl></div>

        <legend id='X78qL'><style id='X78qL'><dir id='X78qL'><q id='X78qL'></q></dir></style></legend>

                <tbody id='X78qL'></tbody>
              <tfoot id='X78qL'></tfoot>

                <small id='X78qL'></small><noframes id='X78qL'>

                  <bdo id='X78qL'></bdo><ul id='X78qL'></ul>
                  本文介绍了WPF:将集合与集合绑定到具有组的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有时 WPF 对我来说太复杂了.我的Window1"拥有一组Group".Group"是一个包含Person"集合的类.最后,这应该是一个联系人列表.我只想在 ListBox 中显示组及其人员,其中列表组的组名等于我的类组"的名称属性.

                  sometimes WPF is too complex for me. I've got my "Window1" holding a collection of "Group"s. "Group" is a class with a collection of "Person"s. In the end this should be a contact list. What I simply want to do is to show the groups with its person in a ListBox, where the group name of the list groups equals the Name Property of my class "Groups".

                  我已尝试将 CollectionViewSource 绑定到集合".组显示正确,但列表中的项目与组名称相同.所以每个组只有一项:它的组名.

                  I've tried with a CollectionViewSource bound to the "Collection". The groups are shown correct, but the items of the list are equal to the group names. So each group has only one item: its group name.

                  这里的许多示例显示了只有一个集合的项目分组.我可以做的是将组名设置为Person"的属性.但后来我数不清(这确实是必要的):- 每组有多少人- 其中有多少人拥有状态"在线".

                  Many examples here show the grouping of items with only one collection. What I can do is to set the group name as Property of "Person". But then I can't count (and that is really neccessary): - how many persons are in each group - how many of that persons have the "Status" "Online".

                  我在组"类中使用 linq 来计算它.感谢您提供任何帮助我开始的建议.

                  I use linq in the "Group" class to count that. Thanks for any advice helping me to get started.

                  推荐答案

                  好吧,我不确定这是否是您想要实现的目标,但您可以尝试以下方法:

                  Well, I am not sure if this is what you want achieve but here is a way that you can try:

                  假设你的课程是这样的:

                  Assuming your classes are like these:

                  public class Group
                  {
                      public string Name { get; set; }
                      public List<Contact> Contacts { get; set; }
                  }
                  
                  public class Contact
                  {
                      public string Name { get; set; }
                      public bool IsOnline { get; set; }
                  }
                  

                  您可以将 ListBox.ItemTemplate 设置为另一个 ListBox 绑定到 Contacts 属性,例如:

                  You can set ListBox.ItemTemplate as another ListBox bind to Contacts property, like:

                  <CollectionViewSource x:Key="groups" Source="{Binding}" >
                      <CollectionViewSource.GroupDescriptions>
                          <PropertyGroupDescription PropertyName="Name" />
                      </CollectionViewSource.GroupDescriptions>
                  </CollectionViewSource>
                  
                  <DataTemplate x:Key="groupTemplate" DataType="Group">
                      <StackPanel Orientation="Horizontal">
                          <TextBlock Text="{Binding Name}" />
                      </StackPanel>
                  </DataTemplate>
                  
                  <ListBox ItemsSource="{Binding Source={StaticResource groups}}">
                      <ListBox.GroupStyle>
                          <GroupStyle HeaderTemplate="{StaticResource groupTemplate}" />
                      </ListBox.GroupStyle>
                      <ListBox.ItemTemplate>
                          <DataTemplate DataType="Contact">
                              <ListBox ItemsSource="{Binding Contacts}">
                                  <ListBox.ItemTemplate>
                                      <DataTemplate DataType="Contact">
                                          <TextBlock Text="{Binding Name}" />
                                      </DataTemplate>
                                  </ListBox.ItemTemplate>
                              </ListBox>
                          </DataTemplate>
                      </ListBox.ItemTemplate>
                  </ListBox>
                  

                  您必须稍微设置内部列表框的样式.

                  You have to style the inner listbox a little bit.

                  Edit:使用TreeView

                  <DataTemplate DataType="Contact">
                     <TextBlock Text="{Binding Name}" />
                  </DataTemplate>
                  
                  <TreeView ItemsSource="{Binding Source={StaticResource groups}}">
                      <TreeView.ItemTemplate>
                          <HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding Contacts}">
                              <TextBlock Text="{Binding Name}" />
                          </HierarchicalDataTemplate>
                      </TreeView.ItemTemplate>
                  </TreeView>
                  

                  这篇关于WPF:将集合与集合绑定到具有组的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:WPF 列表框复制到剪贴板 下一篇:WPF/C# 将自定义对象列表数据绑定到 ListBox?

                  相关文章

                  1. <small id='kOUE3'></small><noframes id='kOUE3'>

                  2. <i id='kOUE3'><tr id='kOUE3'><dt id='kOUE3'><q id='kOUE3'><span id='kOUE3'><b id='kOUE3'><form id='kOUE3'><ins id='kOUE3'></ins><ul id='kOUE3'></ul><sub id='kOUE3'></sub></form><legend id='kOUE3'></legend><bdo id='kOUE3'><pre id='kOUE3'><center id='kOUE3'></center></pre></bdo></b><th id='kOUE3'></th></span></q></dt></tr></i><div id='kOUE3'><tfoot id='kOUE3'></tfoot><dl id='kOUE3'><fieldset id='kOUE3'></fieldset></dl></div>
                      <bdo id='kOUE3'></bdo><ul id='kOUE3'></ul>
                    <legend id='kOUE3'><style id='kOUE3'><dir id='kOUE3'><q id='kOUE3'></q></dir></style></legend>
                  3. <tfoot id='kOUE3'></tfoot>