无扩展即时为WPF / MVVM搜索 [英] Reactive Extensions Instant Search for WPF/MVVM

查看:174
本文介绍了无扩展即时为WPF / MVVM搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的地方,当你键入,结果在另一个ListBox中立即出现一个文本框。我一直在寻找一个无扩展(RX)的例子,而我发现使用的人的 Observable.FromEventPattern()连同文本框的框TextChanged 事件:

I would like to implement a TextBox where, as you type, results appear instantly in another ListBox. I've been looking for examples with Reactive Extensions (Rx), and all of the ones I found use Observable.FromEventPattern() together with the TextBox's TextChanged event:

  • Implementing simple instant search with Rx (Reactive Extensions) (this is actually WinForms)
  • An Rx-Enabled WPF AutoComplete TextBox - Part 1/2
  • Search on TextChanged with Reactive Extensions

我使用。用WPF MVVM,所以我不能确切地进入文本框或直接的事件。

I'm using WPF with MVVM so I can't exactly access the TextBox or its events directly.

我也偶然发现的this~~MD~~aux回答它展示了如何 Observable.FromEventPattern()可以在MVVM环境中使用,但我希望的东西比捕捉每一个的PropertyChanged 事件越好。

I've also stumbled upon this answer which shows how Observable.FromEventPattern() can be used in an MVVM setting, but I was hoping for something better than capturing every single PropertyChanged event.

什么是一个很好的替代 Observable.FromEventPattern()可与WPF / MVVM很好地工作?

What is a good alternative to Observable.FromEventPattern() that can work nicely with WPF/MVVM?

推荐答案

得到了与 ReactiveUI 这方面的工作。

Got this working with ReactiveUI.

解决方案是基于一个博客文章 ReactiveUI ,但代码有一点点的过时的。我在主持的到位桶四通八达的交通网络的解决方案。它采用ReactiveUI 5.5.1。

The solution is based on a blog post at ReactiveUI, but the code there is a little bit out of date. I am hosting the solution on BitBucket for ease of access. It uses ReactiveUI 5.5.1.

这是该解决方案的视图模型。 SEARCHTEXT 在View绑定到文本框当用户键入他的查询,而的SearchResult 绑定到的ListBox 显示结果。

This is the ViewModel from that solution. SearchText is bound to a TextBox in the View where the user types his query, while SearchResults is bound to a ListBox displaying the results.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Reactive.Linq;
using System.Windows.Input;

using ReactiveUI;

namespace ReactiveExtensionsSearch
{
    public class MainWindowViewModel : ReactiveObject
    {
        private string[] _repository = new string[]
            { "Mario", "Maria", "Gigi", "Jack", "James", "Jeremy" };
        private ObservableAsPropertyHelper<ObservableCollection<string>> _searchResults;
        private string _searchText;
        private ICommand _executeSearchCommand;

        public string SearchText
        {
            get
            {
                return _searchText;
            }
            set
            {
                this.RaiseAndSetIfChanged(ref _searchText, value);
            }
        }

        public ObservableCollection<string> SearchResults
        {
            get
            {
                return _searchResults.Value;
            }
        }

        public MainWindowViewModel()
        {
            var executeSearchCommand = new ReactiveCommand();
            var results = executeSearchCommand.RegisterAsyncFunction(s => { return ExecuteSearch(s as string); });
            _executeSearchCommand = executeSearchCommand;

            this.ObservableForProperty<MainWindowViewModel, string>("SearchText")
                .Throttle(TimeSpan.FromMilliseconds(800))
                .Select(x => x.Value)
                .DistinctUntilChanged()
                .Where(x => !string.IsNullOrWhiteSpace(x))
                .Subscribe(_executeSearchCommand.Execute);

           _searchResults = new ObservableAsPropertyHelper<ObservableCollection<string>>(results, _ => raisePropertyChanged("SearchResults"));
        }

        private ObservableCollection<string> ExecuteSearch(string searchText)
        {
            var q = from s in _repository where s.ToLower().StartsWith(searchText.ToLower()) select s;
            var results = new ObservableCollection<string>(q);
            return results;
        }
    }
}

这篇关于无扩展即时为WPF / MVVM搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆