【问题标题】:items are not displayed after grouping in ItemsControl在 ItemsControl 中分组后不显示项目
【发布时间】:2012-07-16 10:44:18
【问题描述】:

我已将分组添加到 ItemsControl:

        <ItemsControl Style="{StaticResource SellingDashboardToDosList}" Grid.Row="2" BorderThickness="1" Background="#C7E8F8" HorizontalAlignment="Stretch" ItemsSource="{Binding ToDoList}" >
            <ItemsControl.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="GroupItem">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="GroupItem">
                                        <GroupBox Header="{Binding Name}">
                                            <ItemsPresenter />
                                        </GroupBox>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ItemsControl.GroupStyle>
        </ItemsControl>

现在我只看到空的 GroupBox。我使用 Snoop 工具来探索应用程序,我发现 GroupBox ItemPresenters 是空的!可能是什么原因?

如果我从 ItemsControl(ItemsControl.GroupStyle 元素)中删除分组,那么一切正常,我会再次看到所有项目。我不需要对底层数据上下文进行任何更改即可查看所有项目。数据上下文(ItemsSource binging)的类型是 CollectionViewSource

绑定跟踪已打开,但我没有看到任何绑定错误。

【问题讨论】:

  • 样式适用于我的示例数据。看看分组字段数据。
  • 我应该寻找什么?现在组的 ItemsPresenters 是空的。为什么?
  • 你能上传一些你用一些数据尝试过的示例代码吗?
  • 不能快速完成。我稍后会尝试这样做。

标签: wpf grouping itemscontrol


【解决方案1】:

似乎 ItemsControl 样式覆盖了 ItemsControl.Template 属性。一旦该样式被覆盖,问题就解决了。

【讨论】:

  • 为了完成解决方案,最好使用正确的“ItemsPanelTemplate”设置“ItemsPanel”,而不是覆盖 ItemControl 模板。
【解决方案2】:

您必须先对数据进行分组。使用 CollectionViewSource 来做到这一点:

<CollectionViewSource x:Key="Data" Source="{StaticResource SellingDashboardToDosList}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="PropertyNameToGroupBy"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

只有这样您才能执行以下操作:

<ItemsControl ItemsSource="{Biding Source={StaticResource Data}}" ...

【讨论】:

  • 数据上下文是CollectionViewSource,只是它是用C#而不是XAML生成的。很抱歉没有早点提及。感谢您愿意提供帮助。
  • 是的。问题出在样式上,而不是数据源上。
猜你喜欢
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多