任何人都可以帮我查明我的文本框的文本是否在 MVVM 模式(使用 WPF)中一秒钟没有改变。
在我的 ViewModel 中,我有一个属性
public String SearchText
{
get
{
return _searchString;
}
set
{
_searchString = value;
_functionProvidersView.Refresh();
}
}
哪里_functionProvidersView
类型为 ObservableCollection<FunctionProviderViewModel>
.我希望只有当文本一秒钟没有改变时才会刷新。
我试图用 System.Threading
解决这个问题但这并没有解决问题,有人有简单的解决方案吗?
编辑:基于SearchText
我的收藏被过滤了。而且因为我的过滤器机制需要一些时间,所以我希望只有在用户一秒钟内没有更改文本框文本(没有输入任何内容)时才会进行过滤。
TextBox 属性 UpdateSourceTrigger
设置为 PropertyChanged
.我可以将其设置为 LostFocus
但这并不是我想要的...
最佳答案
一种方法是使用 timer延迟刷新调用:
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 秒后调用
关于c# - 找出文本框文本是否在 mvvm 模式中没有改变一秒钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24052351/