Reputation: 1959
I am trying to get my objects sorted and grouped using the ListBox's Grouping feature.
The whole sorting part of the feature works great, but when trying to bind a property in the GroupStyle, it just doesn't work.
I followed an article on MSDN and have done exactly the same procedure as the author, but it still doesn't work.
Here is the object which are in a List and then bound to the ListBox.ItemsSource
public class SearchResult
{
public string name { get; set; }
public ImageBrush image { get; set; }
public Guid result { get; set; }
public resultType type { get; set; }
}
It's the type
property I want to group, which is an Enum and looks like this
public enum resultType
{
Artist,
Album,
Track,
Playlist
}
As for my XAML-code, here is the listbox itself.
<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>
Binding to the type property just doesn't seem to do anything!
And I've made sure the objects do have a value in the property.
And here is the last of the relevant code, it's where you add/remove the grouping:
CollectionView myView;
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
myView = (CollectionView)CollectionViewSource.GetDefaultView(SearchResultContainer.ItemsSource);
if (myView.CanGroup == true)
{
PropertyGroupDescription groupDescription
= new PropertyGroupDescription("type");
myView.GroupDescriptions.Add(groupDescription);
}
else
return;
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
myView = (CollectionView)CollectionViewSource.GetDefaultView(SearchResultContainer.ItemsSource);
myView.GroupDescriptions.Clear();
}
As I said, I followed the author's every move. Any suggestions would be super appreciated!
Upvotes: 0
Views: 655
Reputation: 81253
By default DataContext
of GroupStyle
is CollectionViewGroup which contains Name
property to which you can bind to which will get you the value of property on which you grouped the items.
<TextBlock FontWeight="Light" FontSize="15"
Text="{Binding Path=Name}"/>
Upvotes: 2