WPF:滚动项目控制内容固定标题

2023-11-22

是否可以使用 WPF 的 ItemsControl 执行类似的操作:Demo

我正在尝试冻结 GroupedItems 而不是 GridView 列。

enter image description here

资源:

<Window.Resources>
    <CollectionViewSource x:Key="data" Source="{Binding}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Date"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

列表显示:

<ListView Grid.Column="0" ItemsSource="{Binding Source={StaticResource data}}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="Col 1" DisplayMemberBinding="{Binding Col1}" Width="100"/>
                <GridViewColumn Header="Col 2" DisplayMemberBinding="{Binding Col2}" Width="100"/>
                <GridViewColumn Header="Col 3" DisplayMemberBinding="{Binding Col3}" Width="100"/>
            </GridView.Columns>
        </GridView>
    </ListView.View>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid Grid.Row="0">
                                        <TextBlock Background="Beige" FontWeight="Bold" Text="{Binding Path=Name, StringFormat={}{0}}"/>
                                    </Grid>
                                    <DockPanel Grid.Row="1">
                                        <ItemsPresenter Grid.Row="2"></ItemsPresenter>
                                    </DockPanel>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

背后代码:

public MainWindow()
{
    InitializeComponent();

    List<String> colList1 = new List<string>() { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7" };
    List<String> colList2 = new List<string>() { "1", "2", "3", "4", "5", "6" };

    ObservableCollection<Data> dataCollection = new ObservableCollection<Data>();

    for (var a = 0; a < 100; a++)
    {
        Random rnd = new Random();
        int min = rnd.Next(5000);
        int rnd1 = rnd.Next(0, 6);
        int rnd2 = rnd.Next(0, 5);

        dataCollection.Add(new Data()
        {
            Date = DateTime.Now.AddMinutes(min).ToString("hh:MM tt"),
            Col1 = colList1[rnd2],
            Col2 = String.Format("Col2: {0}", "X"),
            Col3 = colList2[rnd2]
        });
    }
    this.DataContext = dataCollection;
}

public class Data
{
    public string Date { get; set; }
    public string Col1 { get; set; }
    public string Col2 { get; set; }
    public string Col3 { get; set; }
}

我的解决方案使用共享组标题样式的 TextBlock 覆盖。定位和正确的命中测试是棘手的部分,但我非常有信心这不会因布局或逻辑的微小变化而中断。

我不确定您是否想隐藏 ColumnHeader,但这很简单,除了所描述的内容之外不需要任何其他调整here.

enter image description here

背后代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class FreezingGroupHeader : UserControl
    {
        private double _listviewHeaderHeight;
        private double _listviewSideMargin;

        public FreezingGroupHeader()
        {
            InitializeComponent();

            List<String> colList1 = new List<string>() { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7" };
            List<String> colList2 = new List<string>() { "1", "2", "3", "4", "5", "6" };

            ObservableCollection<Data> dataCollection = new ObservableCollection<Data>();

            Random rnd = new Random();

            for (var a = 0; a < 100; a++)
            {
                int min = rnd.Next(5000);
                int rnd1 = rnd.Next(0, 6);
                int rnd2 = rnd.Next(0, 5);

                dataCollection.Add(
                    new Data()
                    {
                        Date = DateTime.Now.AddMinutes(min).ToString("hh:MM tt"),
                        Col1 = colList1[rnd2],
                        Col2 = String.Format("Col2: {0}", "X"),
                        Col3 = colList2[rnd2]
                    }
                );
            }
            this.DataContext = dataCollection;

            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Position frozen header
            GetListViewMargins(this.listview1);

            Thickness margin = this.frozenGroupHeader.Margin;
            margin.Top = _listviewHeaderHeight;
            margin.Right = SystemParameters.VerticalScrollBarWidth + _listviewSideMargin;
            margin.Left = _listviewSideMargin;

            this.frozenGroupHeader.Margin = margin;

            UpdateFrozenGroupHeader();
        }

        private void listview1_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            UpdateFrozenGroupHeader();
        }

        /// <summary>
        /// Sets text and visibility of frozen header
        /// </summary>
        private void UpdateFrozenGroupHeader()
        {
            if (listview1.HasItems)
            {
                // Text of frozenGroupHeader
                GroupItem group = GetFirstVisibleGroupItem(this.listview1);
                if (group != null)
                {
                    object data = group.Content;
                    this.frozenGroupHeader.Text = data.GetType().GetProperty("Name").GetValue(data, null) as string;  // slight hack
                }
                this.frozenGroupHeader.Visibility = Visibility.Visible;
            }
            else
                this.frozenGroupHeader.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Sets values that will be used in the positioning of the frozen header
        /// </summary>
        private void GetListViewMargins(ListView listview)
        {
            if (listview.HasItems)
            {
                object o = listview.Items[0];
                ListViewItem firstItem = (ListViewItem)listview.ItemContainerGenerator.ContainerFromItem(o);
                if (firstItem != null)
                {
                    GroupItem group = FindUpVisualTree<GroupItem>(firstItem);
                    Point p = group.TranslatePoint(new Point(0, 0), listview);
                    _listviewHeaderHeight = p.Y; // height of columnheader
                    _listviewSideMargin = p.X; // listview borders
                }
            }
        }

        /// <summary>
        /// Gets the first visible GroupItem in the listview
        /// </summary>
        private GroupItem GetFirstVisibleGroupItem(ListView listview)
        {
            HitTestResult hitTest = VisualTreeHelper.HitTest(listview, new Point(5, _listviewHeaderHeight + 5));
            GroupItem group = FindUpVisualTree<GroupItem>(hitTest.VisualHit);
            return group;
        }


        /// <summary>
        /// walk up the visual tree to find object of type T, starting from initial object
        /// http://www.codeproject.com/Tips/75816/Walk-up-the-Visual-Tree
        /// </summary>
        private static T FindUpVisualTree<T>(DependencyObject initial) where T : DependencyObject
        {
            DependencyObject current = initial;

            while (current != null && current.GetType() != typeof(T))
            {
                current = VisualTreeHelper.GetParent(current);
            }
            return current as T;
        }

        public class Data
        {
            public string Date { get; set; }
            public string Col1 { get; set; }
            public string Col2 { get; set; }
            public string Col3 { get; set; }
        }
    }
}

Xaml:

<UserControl x:Class="WpfApplication1.FreezingGroupHeader"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
           >

    <UserControl.Resources>
        <CollectionViewSource x:Key="data" Source="{Binding}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Date"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <Style x:Key="GroupHeaderStyle1" TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="Beige" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </UserControl.Resources>

    <Grid>
        <ListView x:Name="listview1" Grid.Column="0" ItemsSource="{Binding Source={StaticResource data}}" ScrollViewer.ScrollChanged="listview1_ScrollChanged" >
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Col 1" DisplayMemberBinding="{Binding Col1}" Width="100"/>
                        <GridViewColumn Header="Col 2" DisplayMemberBinding="{Binding Col2}" Width="100"/>
                        <GridViewColumn Header="Col 3" DisplayMemberBinding="{Binding Col3}" Width="100"/>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition  Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>
                                            <Grid Grid.Row="0">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"/>
                                                </Grid.ColumnDefinitions>
                                                <TextBlock Style="{StaticResource GroupHeaderStyle1}" Text="{Binding Name, StringFormat={}{0}}"  />
                                            </Grid>
                                            <DockPanel Grid.Row="1">
                                                <ItemsPresenter Grid.Row="2"></ItemsPresenter>
                                            </DockPanel>
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>
        <TextBlock x:Name="frozenGroupHeader" Style="{StaticResource GroupHeaderStyle1}" VerticalAlignment="Top"/>
    </Grid>
</UserControl>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF:滚动项目控制内容固定标题 的相关文章

随机推荐

  • WPF:以 MVVM 方式绑定 TreeView 分步教程

    请参阅下一篇文章 原来这一题内容已被删除 因为没有任何意义 简而言之 我询问如何以 MVVM 方式使用 XmlDataProvider 将 XML 我在解析 DLL 程序集时错误生成 绑定到 TreeView 但后来我明白这种方法是错误的
  • ASP.NET MVC 3 跨所有子视图布局 ViewBag 数据

    无论子视图是什么 我们都需要将动态数据传递到布局文件 例如 我们在布局的标题中显示一些用户特定的数据 我们如何将这些数据传递到布局视图 而无需每个操作都独立提供它 我们应该使用自定义控制器 还是有更好的解决方案 我使用的策略是拥有一个基本视
  • 使用 page.getMetrics() 获取 puppeteer 中的页面加载时间

    我正在尝试使用 puppeteer 来测量一组网站在我的环境中加载的速度 我关注的是网络连接的质量和网络速度 所以我很高兴知道一个页面访问所花费的时间load 对于负载的外行定义 当所有图像和html都被浏览器下载时 通过使用 puppet
  • 如何获取 Spacy 中的所有名词短语

    我是新来的Spacy我想从句子中提取 所有 名词短语 我想知道我该怎么做 我有以下代码 import spacy nlp spacy load en file open E test txt r doc nlp file read for
  • Spring Web MVC、@ModelAttribute 和 @RequestParam 一起使用

    我有一个带有 GET 方法的控制器 如下所示 Controller public class ThingController RequestMapping value Thing html method RequestMethod GET
  • 如何在多个模块中使用 python 日志记录

    我想知道在 Python 应用程序中执行日志记录的标准设置是什么 我正在使用 Logging 类 并且我已经编写了自己的记录器类来实例化 Logging 类 然后我的 main 实例化我的记录器包装类 但是 我的 main 实例化了其他类
  • 如何在Unity3D输入字段UI组件中使用“On Value Change”

    我正在学习有关如何使用 Unity3d 的教程 但我已经走进了死胡同 我相信新版本的 Unity 中发生了一些变化 因为该教程似乎按照我的方式工作得很好 我有一个输入字段 UI 组件 每次更改它时我都想调用 C 函数 根据教程 我只需使用输
  • 使用 PHP 将文件从一台服务器移动到另一台服务器的最佳方法是什么?

    我想设置一个运行 PHP 脚本的 CRON 该脚本又将 XML 文件 保存非敏感信息 从一台服务器移动到另一台服务器 我已获得正确的用户名 密码 并且想要使用 SFTP 协议 这些作业将每天运行 有可能一台服务器是 Linux 另一台服务器
  • iOS:如何使用 Google Drive sdk 库将文件上传到特定的 Google Drive 文件夹

    我将 Google Drive sdk 与我的 iOS 应用程序集成 但我不知道如何将文件上传到 Google 驱动器特定文件夹 这里的代码用于上传文件 但这是将文件上传到我的谷歌驱动器根文件夹 有人分享一个代码来将文件上传到谷歌驱动器特定
  • 使用 Heroku 的 Git 子模块

    问题 我在 Heroku 上有一个 Rails 3 1 应用程序 它将很快需要一堆 第 3 方 子模块 其中一些有子模块 不幸的是 Heroku 缺乏子模块支持 Heroku 网站上的一项建议是将子模块的内容移至主存储库中 here 第一次
  • pip freeze 列出已卸载的软件包

    在 OS X 10 6 8 上 我使用卸载了一个包 至少 pip 告诉我是这样 sudo pip uninstall pkg name 但当我这样做时包裹仍然出现 pip freeze 我再次尝试执行上面的卸载命令 pip 告诉我该软件包尚
  • 将输入文件替换为表单中我自己的按钮

    基本上我想隐藏输入文件并使用按钮来选择表单中的文件 如果我使用
  • onchange 事件是否传播?

    我使用事件委托来侦听 DOM 中较低层的事件 但它不适用于选择框上的 onchange 事件 onchange 事件是在 DOM 中传播还是冒泡 谷歌搜索未能找到确凿的答案 根据规格 change submit reset应该起泡并且foc
  • 查询Android数据库是否存在!

    我已经为我的 android 应用程序创建了一个数据库 其中包含静态数据并且不需要更新 删除功能 因此当应用程序启动时 我想检查数据库是否存在 如果不存在则执行我的 dbAdapter 类 我知道它是一个简单的 if 语句 但我只是想知道查
  • 您建议使用哪个 CPAN 模块将 HTML 转换为纯文本?

    您建议使用哪个 CPAN 模块将 HTML 转换为格式化纯文本 一项严格的要求是该模块必须处理 Unicode 字符 I like HTML 格式文本 and HTML FormatText WithLinks
  • Angular 2 - 如何为动态加载的组件设置 id 属性

    我在用DynamicComponentLoader加载子组件并生成以下 html
  • 在 std::map 和 std::unordered_map 之间进行选择[重复]

    这个问题在这里已经有答案了 现在std有一个真正的哈希映射unordered map 为什么 或何时 我仍然想使用旧的map over unordered map在它实际存在的系统上 是否有任何我无法立即看到的明显情况 As 已经提到过 m
  • R tm removeWords函数不删除单词

    我试图从我构建的语料库中删除一些单词 但它似乎不起作用 我首先遍历所有内容并创建一个数据框 按频率顺序列出我的单词 我使用此列表来识别我不感兴趣的单词 然后尝试创建一个删除单词的新列表 但是 这些词仍然保留在我的数据集中 我想知道我做错了什
  • 在 JAVA 中使用 JSOUP 从 HTML 中提取 CSS 样式

    任何人都可以帮助使用 Java 中的 Jsoup 从 HTML 中提取 CSS 样式 例如在下面的 html 中我想提取 ft00 和 ft01 div style width 931 height 1243 div
  • WPF:滚动项目控制内容固定标题

    是否可以使用 WPF 的 ItemsControl 执行类似的操作 Demo 我正在尝试冻结 GroupedItems 而不是 GridView 列 资源