C# ListBox分组-绑定类型为的标头无效
我正在尝试使用ListBox的分组功能对对象进行排序和分组 该功能的整个排序部分工作得很好,但是当尝试在GroupStyle中绑定属性时,它就是不起作用 我遵循了MSDN上的一个步骤,并完成了与作者完全相同的步骤,但仍然不起作用 下面是列表中的对象,然后绑定到ListBox.ItemsSourceC# ListBox分组-绑定类型为的标头无效,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我正在尝试使用ListBox的分组功能对对象进行排序和分组 该功能的整个排序部分工作得很好,但是当尝试在GroupStyle中绑定属性时,它就是不起作用 我遵循了MSDN上的一个步骤,并完成了与作者完全相同的步骤,但仍然不起作用 下面是列表中的对象,然后绑定到ListBox.ItemsSource public class SearchResult { public string name { get; set; } public ImageBrush image { get;
public class SearchResult
{
public string name { get; set; }
public ImageBrush image { get; set; }
public Guid result { get; set; }
public resultType type { get; set; }
}
这是我想要分组的type
属性,它是一个枚举,如下所示
public enum resultType
{
Artist,
Album,
Track,
Playlist
}
至于我的XAML代码,这里是列表框本身
<ListBox Width="400" MouseDoubleClick="SearchResultContainer_MouseDoubleClick" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Transparent" Height="330" SelectionMode="Extended" VerticalAlignment="Stretch" ItemsSource="{Binding Path=srs}" Margin="0,370,0,0" x:Name="SearchResultContainer">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="50" Background="Transparent" Margin="5">
<Border HorizontalAlignment="Left" Height="50" Width="50" Background="{Binding Path=image}"/>
<TextBlock Text="{Binding Path=name}" VerticalAlignment="Center" FontWeight="Light" Margin="60,0" FontSize="18" Foreground="White"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Light" FontSize="15"
Text="{Binding Path=type}"/> <---- Here is where I'm getting the issue
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
</ListBox.Resources>
</ListBox>
正如我所说,我跟随作者的一举一动。
任何建议都将不胜感激 默认情况下,
GroupStyle
的DataContext
包含Name
属性,您可以绑定到该属性,该属性将获取您对项目进行分组的属性的值
<TextBlock FontWeight="Light" FontSize="15"
Text="{Binding Path=Name}"/>
哦!我没有注意到。非常感谢你!!
<TextBlock FontWeight="Light" FontSize="15"
Text="{Binding Path=Name}"/>