ListView保持ListViewHeaderItem在最前面 [英] ListView Keep ListViewHeaderItem On Top

查看:173
本文介绍了ListView保持ListViewHeaderItem在最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先解释我想达到的目的:

字母A是ListView中的一个ListViewHeaderItem。没有滚动列表的顶部看起来像这样。





在滚动ListViewHeaderItem之后,A与其余项目一起向下移动 -
,但是我怎样才能实现Header作为第一项的种类,直到字母B ist子项目即将到来?我想要实现的行为的一个例子是Microsoft的官方Windows邮件应用程序。它将日期时间保持在最高,直到电子邮件到达前一天写入。



我不知道这个问题是否已经存在,但我不' t知道它是如何被调用的,我不知道该怎么去Google。 解决方案

根据你的描述,你想要的是一个分组的ListView。这里的关键点是使用



欲了解更多信息,请参阅如何将列表或网格中的项目(XAML)简单ListView示例放入 ListView和GridView示例在GitHub上。


I start by explaining what I want to achieve:

The Letter "A" is one ListViewHeaderItem in my Listview. Without Scrolling the top of the List is looking like this.

After I am Scrolling the ListViewHeaderItem "A" is moving downwards with the rest of the items - but how can I achieve that the Header is staying on top as Kind of the first item until the Letter "B" with ist subitems is coming? An example of the behaviour I want to achieve is the official "Mail" app for Windows 10 by Microsoft. It is keeping the datetime at the top until emails are coming which have been written one day earlier.

I don't know if this question is already existing but I don't know how it is called and I don't know what to Google for.

解决方案

According to your description, I think what you want is a grouped ListView. The key points here is using CollectionViewSource as ItemsSource and setting GroupStyle to specify how groups are displayed. Following is a simple sample:

In XAML

<Page.Resources>
    <CollectionViewSource x:Name="groupInfoCVS" IsSourceGrouped="True" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{Binding Source={StaticResource groupInfoCVS}}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Margin="15" Text="{Binding Path=Text}" />
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Background="LightGray">
                            <TextBlock Margin="10" Foreground="Black" Text="{Binding Key}" />
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
</Grid>

And in code-behind

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        List<TestDemo> list = new List<TestDemo>();

        for (int i = 0; i < 6; i++)
        {
            list.Add(new TestDemo { Key = "A", Text = $"Test A {i}" });
            list.Add(new TestDemo { Key = "B", Text = $"Test B {i}" });
        }

        var result = from t in list group t by t.Key;
        groupInfoCVS.Source = result;
    }
}

public class TestDemo
{
    public string Key { get; set; }
    public string Text { get; set; }
}

And it looks like:

For more info, please see How to group items in a list or grid (XAML) and Simple ListView Sample in ListView and GridView sample on GitHub.

这篇关于ListView保持ListViewHeaderItem在最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆