组框 Header 未显示

GroupBox Header Not Showing

我关注了这个 Whosebug 问题,该问题有一个 link 到另一篇文章,关于如何分组数据,但我的 header 没有显示。有人可以指出为什么我的 header 没有显示在我的数据分组中的问题。我看到的文章和我的标记之间唯一不同的是我的数据采用不同的格式,但我无法控制我如何接收它。

我的数据表示:

<MyDetails xmlns="clr-namespace:MyCompany.Mapping;assembly=Mapping" xmlns:scg="clr-
    namespace:System.Collections.Generic;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MyDetails.Details>
        <scg:List x:TypeArguments="MyAttributes" Capacity="64">
            <MyAttributes Bit="0" Channel="1" Name="SomeName" IOAttribute="O" />
        </scg:List>
    </MyDetails.Details>
</MyDetails>

ViewModel 属性 我绑定到:

ObservableCollection<MyDetails> Channels= new 
ObservableCollection<MyDetails>();

MyDetails Class Public 属性(定义为 DataMember):

List<MyAttributes> Details = new List<MyAttributes>();

MyAttributes Class Public 属性(全部定义为 DataMembers):

property string Channel {get; set;}
property string Bit {get; set;}
property string Name {get; set;}
property string Attribute {get; set;}

XAML 资源 Header:

<CollectionViewSource x:Key="MyDetails" Source="{Binding Channels[0].Details}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="Channel" />
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

XAML:

<ItemsControl ItemsSource="{Binding Source={StaticResource MyDetails}}">
<ItemsControl.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <GroupBox Margin="10" >
                                <GroupBox.Header>
                                <TextBlock Text="{Binding Channel}" FontWeight="Bold" FontSize="16" /> <!-- Does not show -->
                                </GroupBox.Header>
                                <ItemsPresenter />
                            </GroupBox>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Bit}" Width="50" />
            <TextBlock Text="{Binding Path=Name}" Width="150" />
            <TextBlock Text="{Binding Path=Attribute}" Width="50" />
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

DataContext 对每个 GroupItem 将是 CollectionViewGroup type which contains information about each group like Items or Name. You need to change binding in a group header to Name,这将是按

分组的值
<GroupBox.Header>
    <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="16" />
</GroupBox.Header>