C# 组合框不显示分组
我正在尝试将组合框中的项目组织为组。为此,我创建了一个具有项目和组名称字符串的对象。然后设置GroupStyle和ItemTemplate以显示这些值。但是,当前,只有项目字符串显示在组合框中(框中有红色边框,表示某种错误) 以下是我的组合框的xaml: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
<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}}"