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);
}
相关推荐
时光追逐者1 天前
C#/.NET/.NET Core技术前沿周刊 | 第 61 期(2025年11.10-11.16)
c#·.net·.netcore
世洋Blog1 天前
Unity面经-List底层原理、如何基于数组、如何扩容、List存储泛型、List有关在内存中的结构
unity·面试·c#·list
PfCoder1 天前
C# async / await 用法以及和Task的关系
c#·多线程·winform·async、await
唐青枫1 天前
.NET Web 应用 Linux 部署全指南:从环境搭建到生产上线
c#·.net
Charles_go2 天前
41、C#什么是单例设计模式
java·设计模式·c#
夏霞2 天前
c# ASP.NET Core SignalR 客户端与服务端自动重连配置指南
开发语言·c#·asp.net
Scout-leaf2 天前
九成九新自用C#入门文档
c#
烛阴2 天前
隐式vs显式:解密C#类型转换的底层逻辑
前端·c#
梦里不知身是客112 天前
kafka作为Sink
c#·linq
猿来是你_L2 天前
C# Dictionary 转换成 List
windows·c#·list