【问题标题】:listview conditional grouping列表视图条件分组
【发布时间】:2016-01-22 10:35:12
【问题描述】:

在我的 WPF 应用程序中,我有一个 ListView 包含部分分组的文档:

myitems.Add(new Data("document_1", "section_1"));
myitems.Add(new Data("document_1", "section_2"));
myitems.Add(new Data("document_2", "one_and_only_section"));
lv.ItemsSource = myitems;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lv.ItemsSource);
view.GroupDescriptions.Clear();
view.GroupDescriptions.Add(new PropertyGroupDescription("document");

这会导致大致看起来像

< document_1
     section_1
     section_2
< document_2
     one_and_only_section

理论上这很好,但是如果所有内容都折叠起来,选择“one_and_only_section”项非常繁琐,因为它需要单击两次(第一次在“document_2”上,第二次在“one_and_only_section”上)。理想情况下,document_2 不应以与document_1 相同的方式分组:

< document_1
     section_1
     section_2
document_2

因此,如果组中只有一个元素,则不应使用扩展器来显示该元素。如果被选中,它应该表现得好像“one_and_only_section”被选中了。

这对ListView 可行吗?

【问题讨论】:

    标签: c# wpf listview grouping


    【解决方案1】:

    我能够使用以下 XAML 代码生成所需的输出:

    <ListView ItemsSource="{Binding Path=ItemsView}">
    
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <ControlTemplate.Resources>
                                            <DataTemplate DataType="{x:Type local:ItemViewModel}">
                                                <TextBlock Text="{Binding Path=Section}" />
                                            </DataTemplate>
                                        </ControlTemplate.Resources>
                                        <Expander Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}">
                                            <Expander.Header>
                                                <StackPanel Margin="0,8,0,0"
                                                            HorizontalAlignment="Stretch"
                                                            Orientation="Horizontal">
                                                    <TextBlock x:Name="Title"
                                                               VerticalAlignment="Center"
                                                               FontWeight="Bold">
                                                        <Run Text="{Binding Path=Name, Mode=OneWay}" />
                                                        <Run Text=" " />
                                                        <Run Text="{Binding Path=Items.Count, Mode=OneWay}" />
                                                    </TextBlock>
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
    
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Items.Count}" Value="1">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <ControlTemplate.Resources>
                                                    <DataTemplate DataType="{x:Type local:ItemViewModel}">
                                                        <TextBlock Text="{Binding Path=Document}" />
                                                    </DataTemplate>
                                                </ControlTemplate.Resources>
                                                <ItemsPresenter />
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>
    

    您可能需要对扩展器样式和数据模板添加一些额外的注意,以使其看起来相似。

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2014-03-05
      • 2015-12-22
      相关资源
      最近更新 更多