【问题标题】:ListBox Group Binding not displaying header列表框组绑定不显示标题
【发布时间】:2015-01-30 01:56:16
【问题描述】:

我一直在关注这个guide(以及其他资源)并让 ListBox 成功地将我的用户列表分组。现在的问题有两个;

  1. 每个组的标题没有显示它的分组依据(目前,只是一个 int)。
  2. 如果可能的话,我希望标题是一个查找。 (我使用的是实体框架,因此可以通过在查询中使用Include(u => u.Ref_Department) 来获取用户列表,从而使其变得多余。)

数据如下;

用户
用户名
id_Department 名称

部门
id部门
部门名称

XAML;

<Grid>
    <Grid.Resources>
        <CollectionViewSource x:Key="lsUsers" Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=_Users}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="id_Department"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Grid.Resources>
    <ListBox ItemsSource="{Binding Source={StaticResource lsUsers}}">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander Header="{Binding id_Department}" IsExpanded="True">
                                        <ItemsPresenter/>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListBox.GroupStyle>
    </ListBox>
</Grid>

_Users 是该类的 List&lt;Users&gt; 属性,无论如何它显然工作正常,它只是由于某种原因掉下来的标题,我真的不明白为什么。

id_Department 下的 &lt;Expander Header... 行上有一个突出显示/警告,上面写着“由于未知数据上下文,无法解析符号 'id_Department'”

更新 如果我将&lt;Expander Header... 绑定更改为Name,它会显示id_Department。不知道它是如何/为什么起作用的,我不确定它对上面的第 1 点有帮助。

【问题讨论】:

    标签: c# wpf xaml listbox


    【解决方案1】:

    在使用 GroupStyle 时,您只有以下绑定属性:
    Name: 这是根据
    ItemCount:进行分组的组属性的名称> 这是每个分组记录的行数

    如果您想要基于分组项目的另一个属性,您应该使用Converter,但是在使用时:

    {Binding Converter="{StaticResource yourConverter}"
    

    考虑Converter 的值是CollectionViewGroup,它是分组基础项中的对象或行的列表。

    【讨论】:

      【解决方案2】:

      正如@safi 所暗示的那样,因为GroupItem 是要绑定的样式,所以您只有NameItemCount 属性可以绑定。

      听起来您正在尝试按部门分组,但希望查看部门名称而不是 ID。因此,您应该将字符串 Department 属性添加到您的 Users 类并在其上进行分组,而不是部门 ID 属性。

      您可以通过更改集合视图的SortDescriptions 来控制组的顺序。例如,如果您想按部门名称分组但仍按 ID 顺序而不是按字母顺序列出部门,则按名称分组并按 ID 排序,如下所示:

      ICollectionView view = CollectionViewSource.GetDefaultView(_Users);
      view.GroupDescriptions.Add(new PropertyGroupDescription("DepartmentName"));
      view.SortDescriptions.Add(new SortDescription("DepartmentId", ListSortDirection.Ascending));
      view.SortDescriptions.Add(new SortDescription("UserName", ListSortDirection.Ascending));
      

      我在这里使用了不同的属性名称,但你明白了。

      【讨论】:

        猜你喜欢
        • 2015-12-29
        • 2013-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        • 2015-10-23
        • 2011-11-05
        相关资源
        最近更新 更多