简体   繁体   English

C# WPF 列表框组标题样式

[英]C# WPF Listbox Group Header Style

I'm using a Listbox to show multiple items.我正在使用列表框来显示多个项目。 The items are grouped.项目被分组。 Now I want to edit the header style but the only thing happens is that the text isn't shown anymore.现在我想编辑标题样式,但唯一发生的事情是不再显示文本。

Thats my xaml:那是我的xaml:

<ListBox x:Name="lbTreatments" HorizontalAlignment="Left" Height="473" Margin="10,37,0,0" VerticalAlignment="Top" Width="309" FontSize="13">
    <ListBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding subcategoryName}" FontWeight="Bold"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And thats my code to group the items:这就是我对项目进行分组的代码:

ICollectionView view = CollectionViewSource.GetDefaultView(treatmentsCategory);
view.GroupDescriptions.Add(new PropertyGroupDescription("subcategoryName"));
view.SortDescriptions.Add(new SortDescription("subcategoryName", ListSortDirection.Ascending));
lbTreatments.ItemsSource = view;

The items are grouped but the header text is missing.项目已分组,但标题文本丢失。 If I delete the Groupstyle from xaml the text will be shown.如果我从 xaml 中删除 Groupstyle,则会显示文本。 Can anybode help me please?任何人都可以帮助我吗?

MSDN says: MSDN说:

Each group is of type CollectionViewGroup.每个组都是 CollectionViewGroup 类型。

The actual data type is CollectionViewGroup which doesn't have a subcategoryName property so the binding will fail.实际数据类型是CollectionViewGroup ,它没有subcategoryName属性,因此绑定将失败。 Instead you have to use the Name property, which will already be set to the value from subcategoryName .相反,您必须使用Name属性,该属性已设置为来自subcategoryName的值。

As such, use this...因此,使用此...

<TextBlock Text="{Binding Name}"/>

...in your header template to get the group name. ...在您的标题模板中获取组名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM