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);
}
相关推荐
Kiyra1 天前
WebSocket vs HTTP:为什么 IM 系统选择长连接?
分布式·websocket·网络协议·http·设计模式·系统架构·wpf
酩酊仙人1 天前
ABP+Hangfire实现定时任务
后端·c#·asp.net·hangfire
阿蒙Amon1 天前
C#每日面试题-属性和特性的区别
java·面试·c#
要记得喝水1 天前
某公司C#-WPF面试题-来自nowcoder(含答案和解析)--2
c#·wpf
Joker 0071 天前
Linux nohup命令实战指南
linux·运维·wpf
爱敲点代码的小哥1 天前
类型转换 递归算法 编译错误 装箱和拆箱 知识点
开发语言·c#
时光追逐者1 天前
一个 WPF 开源、免费的 SVG 图像查看控件
开源·c#·.net·wpf
江沉晚呤时1 天前
构建智能代理的利器:深入解析 Microsoft Agent Framework
开发语言·c#
武藤一雄1 天前
C# 中线程安全都有哪些
后端·安全·微软·c#·.net·.netcore·线程
wuguan_1 天前
C#递推算法
算法·c#·递推算法