WPF组合框延迟滤波 [英] WPF ComboBox delayed filtering

查看:98
本文介绍了WPF组合框延迟滤波的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情况:
有组合框和过滤器文本框,然后用户在文本框中组合框项目源的文本使用过滤文本进行更新。一切正常,但出现过滤每一个类型的信。欲过滤发生之前添加延迟(而用户键入滤波器不applyed)。什么是做到这一点的方式simpliest?

Consider following situation: there is ComboBox and a filter TextBox, then user types a text in a text box ComboBox items source is updated using filter text. Everything works, but filtering occurs on every typed letter. I want to add a delay before filtering occurs (filter is not applyed while user is typing). What is the simpliest way to do it?

推荐答案

这种操作的介绍,其中每次用户进入一个新的角色定时器的最常用的方法你的时间跨度得到的重置,但比x秒如果是较长然后执行在code。

The most used way of doing this is introducing a timer where everytime the user enters a new character your timespan get's reset but if it is longer than x seconds then execute the code.

记住要做到这一点异步因此,如果用户开始再次键入,而你正在执行搜索,你可以取消异步调用的信息,现在已经过时。

Remember to do it async so that if the user starts typing again while you are performing a search you can cancel the async call as that information will now be outdated.

如果您使用的是视图模型只是改变textbox1_TextChanged到相应属性的setter

If you are using a viewmodel just change textbox1_TextChanged to the appropriate Properties setter

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (!tmr.Enabled)
        {
            tmr.Enabled = true;
            tmr.Start();
        }


        TimeSinceType = DateTime.Now;

    }

public DateTime TimeSinceType { get; set; }

protected void Load()
{
      tmr = new Timer();
      tmr.Interval = 200;
      tmr.Elapsed += new ElapsedEventHandler(tmr_Elapsed);
}

void tmr_Elapsed(object sender, ElapsedEventArgs e)
{
    if ((DateTime.Now - TimeSinceType).Seconds > .5)
    {
        Dispatcher.BeginInvoke((Action)delegate()
        {
            //LoadData();
            tmr.Stop();
        });
    }
}

这篇关于WPF组合框延迟滤波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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