WPF CollectionViewSource Grouping

I use CollectionViewSource to group my data. According to my data, I have Property1 and Property2 that I need to group.

The only condition is that I do not want a subgroup of another group. Therefore, when I group these two properties, I do not want to have it so that Property2 , because the subgroup of the group is Property1 .

The reason I want this is because I need a header that shows the following information:

Title:

 <TextBlock.Text> <MultiBinding StringFormat="Property1: {0}, Property2: {1}"> <Binding Path="Property1"/> <Binding Path="Property2"/> </MultiBinding> </TextBlock.Text> 

I tried this with my CollectionViewSource, but could not "merge" the group and subgroup together:

 <CollectionViewSource x:Key="myKey" Source="{Binding myDataSource}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Property1" /> <PropertyGroupDescription PropertyName="Property2" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> 

Is it possible to group two properties together? Something like below?

 <CollectionViewSource x:Key="myKey" Source="{Binding myDataSource}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Property1,Property2" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> 
+6
wpf grouping collectionviewsource
source share
2 answers

Instead of creating another new property in your object, you can also get some tricks in the converter. Dot ('.') Passes the entire object to your converter. Thus, you can make any logical algorithm there instead of creating a new property in the original object.

 <CollectionViewSource x:Key="myKey" Source="{Binding myDataSource}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="." Converter="{StaticResource Property1AndProperty2}" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> 

On your converter, something like this:

 public class WidthAndHeightMixer : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is YourObject) { return (value as YourObject).Property1 + (value as Inventory).Property2 } } ...... 
+19
source share

You can combine properties into one property of a data object. For example:

 public class Person { public Person() { IsActive = true; } public string FirstName { get; set; } public string LastName { get; set; } public Boolean IsActive { get; set; } public string LastNameIsActive { get { return LastName + IsActive.ToString(); } } } 
 <Grid.Resources> <CollectionViewSource x:Key="view" Source="{StaticResource persons}"> <CollectionViewSource.SortDescriptions> <cm:SortDescription PropertyName="LastName" Direction="Ascending"/> <cm:SortDescription PropertyName="IsActive" Direction="Ascending"/> </CollectionViewSource.SortDescriptions> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="LastNameIsActive"/> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> </Grid.Resources> <ListView ItemsSource="{Binding Source={StaticResource view}}"> <ListView.View> <GridView> <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}"/> <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}"/> </GridView> </ListView.View> <ListView.GroupStyle> <GroupStyle > <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock Background="Gray" DataContext="{Binding Items}"> <TextBlock.Text> <MultiBinding StringFormat="Is Active: {0} Last Name: {1}"> <Binding Path="IsActive"/> <Binding Path="LastName"/> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ListView.GroupStyle> </ListView> 

+2
source share

All Articles