WPF CollectionViewSource分组

Mil*_*les 6 wpf grouping collectionviewsource

我正在使用a CollectionViewSource来分组我的数据.在我的数据中,我有Property1并且Property2我需要分组.

唯一的规定是我不想要另一组的子组.因此,当我按这两个属性进行分组时,我不希望它Property2因为一个子组的Property1's group.

我想要这个的原因是因为我需要一个显示以下信息的标题:

标题:

<TextBlock.Text>
  <MultiBinding StringFormat="Property1: {0}, Property2: {1}">
    <Binding Path="Property1"/>
    <Binding Path="Property2"/>
  </MultiBinding>
</TextBlock.Text>
Run Code Online (Sandbox Code Playgroud)

我用我的CollectionViewSource尝试了这个,但是无法将组和子组"组合"在一起:

<CollectionViewSource x:Key="myKey" Source="{Binding myDataSource}">
  <CollectionViewSource.GroupDescriptions>
    <PropertyGroupDescription PropertyName="Property1" />
    <PropertyGroupDescription PropertyName="Property2" />
  </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)

是否可以将两个属性组合在一起?像下面的东西?

<CollectionViewSource x:Key="myKey" Source="{Binding myDataSource}">
  <CollectionViewSource.GroupDescriptions>
    <PropertyGroupDescription PropertyName="Property1,Property2" />
  </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)

yan*_*cyn 20

实际上,您可以在转换器上使用一些技巧,而不是在对象中创建另一个新属性.点('.')将整个对象传递给转换器.因此,您可以在那里执行任何逻辑算法,而不是在原始对象中创建新属性.

<CollectionViewSource x:Key="myKey" Source="{Binding myDataSource}">
    <CollectionViewSource.GroupDescriptions>
         <PropertyGroupDescription PropertyName="." 
                     Converter="{StaticResource Property1AndProperty2}" />
     </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)

你的转换器是这样的:

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
            }
      }
      ......
Run Code Online (Sandbox Code Playgroud)