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);
}
相关推荐
hhb_6183 小时前
C#高性能异步编程实战与底层原理深度解析
开发语言·c#
beyond谚语3 小时前
反射、特性和依赖注入
c#
Tiger_shl4 小时前
C# 托管对象、非托管对象 讲解
开发语言·c#
LF男男5 小时前
Action- C# 内置的委托类型
java·开发语言·c#
2501_930707788 小时前
使用C#代码在 PowerPoint 中创建组合图表
开发语言·c#·powerpoint
Full Stack Developme10 小时前
Hutool DFA 教程
开发语言·c#
xiaoshuaishuai811 小时前
【无标题】
开发语言·windows·c#
SunnyDays101111 小时前
C# 如何快速比较 Word 文档并显示差异
c#·对比 word 文档·比较 word 文档
LF男男11 小时前
TouchPad(单例)
unity·c#
武藤一雄1 天前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore