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);
}
相关推荐
雨疏风骤124017 分钟前
【FreeRTOS】任务、任务状态
开发语言·stm32·c#·rtos
松☆24 分钟前
Flutter 与 OpenHarmony 深度集成:自定义 MethodChannel 插件开发全指南
flutter·wpf
️公子2 小时前
传奇游戏集成系统
游戏·c#
玩泥巴的2 小时前
强的飞起的 Roslyn 编译时代码生成,实现抽象类继承与依赖注入的自动化配置
c#·.net·代码生成·roslyn
mudtools2 小时前
强的飞起的 Roslyn 编译时代码生成,实现抽象类继承与依赖注入的自动化配置
c#·.net
Aevget2 小时前
界面控件DevExpress WPF中文教程:Data Grid - 虚拟源限制
hadoop·wpf·界面控件·devexpress·ui开发
听风吟丶3 小时前
Java 微服务日志实战:ELK+SkyWalking 构建全链路日志监控与智能告警体系某电商平台曾因日志问题陷入
wpf
️公子3 小时前
无人直播系统-黑客主题
人工智能·c#·visual studio
她说彩礼65万4 小时前
WPF 样式
大数据·hadoop·wpf
c#上位机4 小时前
halcon图像去噪—中值滤波
图像处理·c#·halcon