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