在xaml中对ListView进行分组

时间:2015-10-30 11:42:33

标签: c# wpf windows xaml winrt-xaml

任何人都可以解释一下如何使用内容视图源在xaml中实现分组概念。

这是我的列表项代码,我需要创建一个手风琴,即展开全部并折叠全部:

<Page
x:Class="grouping_accordian.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:grouping_accordian"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
    <CollectionViewSource x:Name="versionViewSource" IsSourceGrouped="True"  />
    <DataTemplate x:Key="sampletemplate">

        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <TextBlock Style="{ThemeResource IncidentSmallTileHeaderStyle}" HorizontalAlignment="Stretch" Text="{Binding Name}" />
            <TextBlock Style="{ThemeResource IncidentSmallTileHeaderStyle}" HorizontalAlignment="Stretch" Grid.Row="1" Text="{Binding Address}" />
            <TextBlock Style="{ThemeResource IncidentSmallTileHeaderStyle}" HorizontalAlignment="Stretch" Grid.Row="2" Text="{Binding Description}" />

        </Grid>
    </DataTemplate>
</Page.Resources>

<Grid >

    <ListView ScrollViewer.VerticalScrollMode="Enabled" HorizontalContentAlignment="Stretch" x:Name="listView" ItemTemplate="{ThemeResource sampletemplate}"  ItemsSource="{Binding Source={StaticResource versionViewSource}}"  HorizontalAlignment="Stretch" Margin="10,81,0,20" VerticalAlignment="Top"  >
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Key}" Foreground="Red" />
                            <Button Click="Button_Click" Content="click"/>
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
        <!--<ListBox x:Name="listBox" ItemTemplate="{ThemeResource sampletemplate}" HorizontalAlignment="Left" Margin="10,81,0,20" VerticalAlignment="Top" Width="700"/>-->
   </ListView>

    <CommandBar VerticalAlignment="Bottom">
        <!--<CommandBar.SecondaryCommands>
            <AppBarButton Label="appbarbutton"/>
        </CommandBar.SecondaryCommands>-->
        <AppBarButton Icon="ViewAll" Label="Expand All" Click="Button_Click2"/>
        <AppBarButton Icon="ClosedCaption" Label="Collapse All" Click="Button_Click_1"/>
    </CommandBar>
</Grid>

找到下面的c-sharp代码,我在列表项中添加数据,现在我已经放置了一个应用栏,使用它我必须创建一个手风琴。

如何使用此代码创建展开全部并折叠所有功能?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace grouping_accordian
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {

        //EmpData empData;
        public MainPage()
        {
            this.InitializeComponent();

           List<Data> dataitems = new List<Data>();
            dataitems.Add(new Data { Name = "Sam", Address = "LA", Description = "police" });
            dataitems.Add(new Data { Name = "Andrew", Address = "washington", Description = "thief" });
            dataitems.Add(new Data { Name = "anderson", Address = "texas", Description = "bank employee" });
            dataitems.Add(new Data { Name = "Willaiam", Address = "LA", Description = "Software Engineering" });
        }

        public class Demo
        {
            public string Key { get; set; }
            public CollectionViewSource CVS { get; set; }
        }

         public class Data
        {
            public string Name { get; set; }
            public string Address { get; set; }
            public string Description { get; set; }
        }
    }
}

0 个答案:

没有答案