Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框不显示分组_C#_.net_Wpf_Xaml_Combobox - Fatal编程技术网

C# 组合框不显示分组

C# 组合框不显示分组,c#,.net,wpf,xaml,combobox,C#,.net,Wpf,Xaml,Combobox,我正在尝试将组合框中的项目组织为组。为此,我创建了一个具有项目和组名称字符串的对象。然后设置GroupStyle和ItemTemplate以显示这些值。但是,当前,只有项目字符串显示在组合框中(框中有红色边框,表示某种错误) 以下是我的组合框的xaml: <ComboBox x:Name="comboBoxProjects" Margin="165,90,28,0" Grid.Column="0" VerticalAlignment="Top" Height="25" IsSync

我正在尝试将组合框中的项目组织为组。为此,我创建了一个具有项目和组名称字符串的对象。然后设置GroupStyle和ItemTemplate以显示这些值。但是,当前,只有项目字符串显示在组合框中(框中有红色边框,表示某种错误)

以下是我的组合框的xaml:

<ComboBox x:Name="comboBoxProjects" Margin="165,90,28,0" Grid.Column="0" VerticalAlignment="Top" Height="25"
    IsSynchronizedWithCurrentItem="True" SelectedIndex="0" Style="{StaticResource ComboBoxDefault}" 
    ItemsSource="{Binding Path=ProjectClientSelections.ProjectGroupItems,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    SelectedItem="{Binding Path=ProjectClientSelections.SelectedProject, UpdateSourceTrigger=PropertyChanged}"> 

    <ComboBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding GroupName}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ComboBox.GroupStyle>

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Project}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>

</ComboBox>


有人知道我哪里出错了吗?

GroupStyle
中,DataContext不是您的项(ItemsSource中包含的类型),而是一个
CollectionViewGroup
对象,它是基于您已分组的项集合形成的。因此,您必须声明到CollectionViewGroup中某个属性的绑定路径,例如,根据您的代码,您可能希望使用
Name
property。看

将您的
GroupStyle.HeaderTemplate更改为:

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

非常感谢你的帮助!
<Window.Resources>
    <CollectionViewSource
        Source="{Binding ProjectClientSelections.ProjectGroupItems}"
        x:Key="GroupedProjectItems">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription
                PropertyName="GroupName" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>
ItemsSource="{Binding Source={StaticResource GroupedProjectItems}}"