首页 > 其他分享 >wpf ObservableCollection筛选功能

wpf ObservableCollection筛选功能

时间:2024-10-01 20:34:34浏览次数:1  
标签:ObservableCollection return FilterSchoolList 列表 Filter SchoolText wpf 筛选

viewmodel中定义原始数据及筛选后的数据,筛选后的数据类型为ICollectionView

//原始数据列表
public ObservableCollection<SchoolOutDto> SchoolList { get; set; }

/// <summary>
/// 筛选数据后的列表
/// </summary>
public ICollectionView FilterSchoolList { get; set; }

//输入框绑定的值
public string SchoolText
{
    get => schoolText; set
    {
        schoolText = value;
        //根据筛选的名称刷新列表
        FilterSchoolList.Refresh();
    }
}

在原始数据获取到数据后绑定筛选后的数据

FilterSchoolList = CollectionViewSource.GetDefaultView(SchoolList);
FilterSchoolList.Filter = Filter;  //此处的Filter为一个回调函数

Filter

private bool Filter(object obj)
{
    var m = obj as SchoolOutDto;
    if (string.IsNullOrEmpty(SchoolText) )
    {
        return true;
    }

    //输入框与选择的列表一致时重置列表(此时为选择了某条数据直接重置)
    if (SelectedSchool != null && SelectedSchool.schoolName==SchoolText)
    {
        return true;
    }

    if (m.schoolName.Contains(SchoolText))
    {
        return true;
    }
    return false;
}

标签:ObservableCollection,return,FilterSchoolList,列表,Filter,SchoolText,wpf,筛选
From: https://www.cnblogs.com/ives/p/18443224

相关文章

  • WPF publish release application
    Rightclickproject/Properties/Publish   ThenclickPublishWizardstepbystep,thenwillcreateasetup.exefileinpublishfolder Clickthesetup.exefileandtheninstalltheapplicationstepbystep,finallyitshowin ControlPanel\AllContro......
  • WPF relative uri
    <ImageGrid.Column="0"Source="pack://application:,,,/WpfApp431;component/Images/1.jpg"RenderOptions.BitmapScalingMode="Fant"/><ImageGrid.Column="1"Source="pack://applic......
  • WPF mutex single instance
    privatestaticMutexmtx=null;publicMainWindow(){boolisCreateNew;stringappName=System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);mtx=newMutex(true,appName,outisCreateNew);if(!isCreate......
  • WPF Calendar DisplayMode SelectionMode FirstDayOfWeek Start End BlackoutDates
    //xaml<Windowx:Class="WpfApp427.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF slider IsSelectionRangeEnabled TickFrequency
    <Windowx:Class="WpfApp426.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • wpf 找不到资源 *.xaml 字典文件异常处理
    找不到字典时设置如下<ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionarySource="/Themes/1.xaml"/><ResourceDictionarySource="/Themes/2.xaml"......
  • WPF MVVM入门系列教程(二、依赖属性)
    说明:本文是介绍WPF中的依赖属性功能,如果对依赖属性已经有了解了,可以浏览后面的文章。 为什么要介绍依赖属性在WPF的数据绑定中,密不可分的就是依赖属性。而MVVM又是跟数据绑定紧密相连的,所以在学习MVVM之前,很有必须先学习一下依赖属性。 依赖属性(DepencencyProperty)是什......
  • WPF Progrss bar stringformat {} {0}% IsDetermined
    //xaml<Windowx:Class="WpfApp425.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • (二)WPF布局
    <Windowx:Class="WpfTest.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.c......
  • WPF下使用FreeRedis操作RedisStream实现简单的消息队列
    RedisStream简介RedisStream是随着5.0版本发布的一种新的Redis数据类型:高效消费者组:允许多个消费者组从同一数据流的不同部分消费数据,每个消费者组都能独立地处理消息,这样可以并行处理和提高效率。阻塞操作:消费者可以设置阻塞操作,这样它们会在流中有新数据添加时被唤醒并开始......