WPF CollectionViewSource 分组

发布于 2024-09-26 21:14:19 字数 1245 浏览 3 评论 0原文

我正在使用 CollectionViewSource 对我的数据进行分组。在我的数据中,我需要对 Property1Property2 进行分组。

唯一的规定是我不想要另一个组的子组。因此,当我按这两个属性进行分组时,我不想将其作为 Property1 组的子组。

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

标题:

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

我已经用我的 CollectionViewSource 尝试过这个,但无法将组和子组“组合”在一起:

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

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

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

I'm using a CollectionViewSource to group my data. In my data, I have Property1 and Property2 that I'm needing to group on.

The only stipulation is that I don't want sub-groups of another group. So, when I group by these two properties, I don't want to have it so that Property2 because a subgroup of Property1's group.

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

Header:

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

I've tried this with my CollectionViewSource but was not able to "combine" 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云柯 2024-10-03 21:14:19

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

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

在你的转换器上是这样的:

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
            }
      }
      ......

Instead of creating another new property in your object actually you can also have some tricks on the converter. Dot ('.') is pass the whole object into you converter. So you can do whatever logic algorithm over there instead of creating a new property in your original object.

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

At 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
            }
      }
      ......
找个人就嫁了吧 2024-10-03 21:14:19

您可以将这些属性合并为数据对象的一个​​属性。例如:

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>

You could combine the properties into one property on your data object. For instance:

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文