谁能帮我找出我的TextBox文本在MVVM模式下(使用WPF)在一秒钟内没有变化。
在我的ViewModel中,我有一个属性
public String SearchText
{
get
{
return _searchString;
}
set
{
_searchString = value;
_functionProvidersView.Refresh();
}
}
_functionProvidersView
Type是哪里ObservableCollection<FunctionProviderViewModel>
。我希望只有在文本一秒钟没有变化的情况下才能进行刷新。
我试图用解决这个问题,System.Threading
但没有成功,有人有简单的解决方案吗?
编辑:基于SearchText
我的收藏集被过滤。并且由于我的过滤器机制需要花费一些时间,因此我希望仅当用户在一秒钟内未更改文本框文本(未键入任何内容)时才进行过滤。
TextBox属性UpdateSourceTrigger
设置为PropertyChanged
。我可以将其设置为,LostFocus
但这并不是我想要的...
一种方法是使用计时器来延迟刷新调用:
public class MainVm
{
System.Threading.Timer timer;
private string searchText = string.Empty;
public string SearchText
{
get { return searchText; }
set
{
searchText = value;
timer.Change(1000, System.Threading.Timeout.Infinite); //reset the timer
}
}
public MainVm()
{
timer = new System.Threading.Timer(RefreshView,
null,
System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}
private void RefreshView(object state)
{
//Here you need to use the dispatcher because the callback is called
//from a non-UI thread
Application.Current.Dispatcher.Invoke(new Action(() =>
_functionProvidersView.Refresh()
);
}
}
通过这种方式,RefreshView
方法将在您停止输入1秒钟后调用
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句