博客
关于我
[WPF 自定义控件]排序、筛选以及高亮
阅读量:435 次
发布时间:2019-03-06

本文共 2112 字,大约阅读时间需要 7 分钟。

如何优化列表以提高查找效率

1. 排序

在WPF中实现数据排序的常用方法是使用 CollectionViewSource。它类似于数据库中的表和视图概念,通过代理实现数据的排序、筛选和分组,而不直接修改原始数据源。以下是使用 CollectionViewSource 实现排序的代码示例:

private readonly CollectionViewSource _viewSource;public HighlightSample(){    InitializeComponent();    _viewSource = new CollectionViewSource    {        Source = Employee.AllExecutives    };    _viewSource.View.Culture = new System.Globalization.CultureInfo("zh-CN");    _viewSource.View.SortDescriptions.Add(new SortDescription(nameof(Employee.FirstName), ListSortDirection.Ascending));    EmployeeElement.ItemsSource = _viewSource.View;}

2. 筛选

CollectionViewSourceView 属性支持过滤功能。以下是实现筛选功能的代码:

_viewSource.View.Filter = (obj) => (obj as Employee).DisplayName.ToLower().Contains(FilterElement.Text);private void OnFilterTextChanged(object sender, TextChangedEventArgs e){    if (_viewSource != null)        _viewSource.View.Refresh();}

3. 高亮显示

为了实现高亮效果,可以使用 TextBlockService 附加属性。以下是实现高亮功能的代码示例:

private static void MarkHighlight(TextBlock target, string highlightText){    var text = target.Text;    target.Inlines.Clear();    if (string.IsNullOrWhiteSpace(text))        return;    if (string.IsNullOrWhiteSpace(highlightText))    {        target.Inlines.Add(new Run { Text = text });        return;    }    while (text.Length > 0)    {        var runText = string.Empty;        var index = text.IndexOf(highlightText, StringComparison.InvariantCultureIgnoreCase);        if (index > 0)        {            runText = text.Substring(0, index);            target.Inlines.Add(new Run { Text = runText, Foreground = _noHighlightBrush });        }        else if (index == 0)        {            runText = text.Substring(0, highlightText.Length);            target.Inlines.Add(new Run { Text = runText });        }        else if (index == -1)        {            runText = text;            target.Inlines.Add(new Run { Text = runText, Foreground = _noHighlightBrush });        }        text = text.Substring(runText.Length);    }}

4. 结语

通过上述方法,我们可以显著提升列表的可用性和效率。虽然高亮功能目前无法直接定义颜色,但可以通过自定义 ToolTipService 来实现更复杂的高亮效果。

5. 参考

6. 源码

转载地址:http://xtyuz.baihongyu.com/

你可能感兴趣的文章
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>