WPF列表视图查询

WPF列表视图查询

查询方法

csharp 复制代码
ICollectionView _collectionView = CollectionViewSource.GetDefaultView(DataItems);
if (_collectionView == null)
{
    return ;
}
_collectionView.Filter = item => item is MetadataItemVO vo && vo.Name.Contains(SearchText, StringComparison.OrdinalIgnoreCase);

源码

View

xml 复制代码
<DockPanel Margin="0,0,100,0">
    <TextBlock VerticalAlignment="Center" Text="元数据项:" />
    <Button
        Margin="10,3,0,3"
        Command="{Binding ResetSearchCommand}"
        DockPanel.Dock="Right"
        Content="重置" />
    <Button
        Margin="10,3,0,3"
        Command="{Binding SearchCommand}"
        DockPanel.Dock="Right"
        Content="查询" />
    <TextBox
        Margin="0,3"
        VerticalContentAlignment="Center"
        Text="{Binding SearchText, Mode=TwoWay}" />
</DockPanel>

Model

csharp 复制代码
public partial class MetadataItemVO
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

ViewModel

csharp 复制代码
private ObservableCollection<MetadataItemVO> _dataItems;
public ObservableCollection<MetadataItemVO> DataItems
{
    get { return _dataItems; }
    set { SetProperty(ref _dataItems, value); }
}

private string _searchText;
public string SearchText
{
    get { return _searchText; }
    set { SetProperty(ref _searchText, value); }
}

public ICommand ResetSearchCommand { get; set; }
public ICommand SearchCommand { get; set; }

private void InitCommands()
{
    ResetSearchCommand = new RelayCommand(ResetSearch);
    SearchCommand = new RelayCommand(Search);
}

private void ResetSearch()
{
    ICollectionView _collectionView = CollectionViewSource.GetDefaultView(DataItems);
    if (_collectionView == null)
    {
        return ;
    }
    _collectionView.Filter = item => true;
}

private void Search()
{
    ICollectionView _collectionView = CollectionViewSource.GetDefaultView(DataItems);
    if (_collectionView == null)
    {
        return ;
    }
    _collectionView.Filter = item => item is MetadataItemVO vo && vo.Name.Contains(SearchText, StringComparison.OrdinalIgnoreCase);
}
相关推荐
溪水西流20 小时前
NodifyEditor Zoom 机制分析
开发语言·c#·avalonia
Never_Satisfied1 天前
在c#中,抛出异常,并指定其message的值
java·javascript·c#
不想看见4041 天前
Word Ladder Ⅱ -- 广度优先搜索--力扣101算法题解笔记
开发语言·c#
全栈小51 天前
【C#】.net 6.0和8.0有什么区别,以及8.0增加了那些功能,这些功能有那些作用
开发语言·c#·.net
qq_297908011 天前
C#印刷线路板ERP进销存报价财务库存贸易生产企业管理系统软件
sqlserver·开源·c#·.net·开源软件
bugcome_com1 天前
C# 循环语句详解:理解常见循环类型与控制语句
c#
数据知道1 天前
PostgreSQL:Citus 分布式拓展,水平分片,支持海量数据与高并发
分布式·postgresql·wpf
JQLvopkk1 天前
能用C#开发AI
开发语言·人工智能·c#
游乐码1 天前
c#类和对象
开发语言·c#
沃码2 天前
【Lively Wallpaper 】插件开发:LivelyProperties.json 全流程实战教程
c#·json·livelywallpaper·视频壁纸