我有一个具有以下三个属性的 viewModel:
string searchString;
ObservableCollection<Company> ListedItems;
ICommand SearchCommand;
它表示我的数据库中可搜索的公司列表。 SearchCommand
根据 searchString
的值搜索数据库,然后用结果填充 ListedItems
。
SearchString
绑定(bind)到一个文本框,而 SearchCommand
绑定(bind)到一个按钮。我想做到这一点,以便当用户在文本框中键入内容时,SearchCommand
会自动执行,而无需用户单击按钮。
目前,我通过我的 viewModel 执行此操作:
public ListViewModel() {
this.PropertyChanged += delegate(object o, PropertyChangedEventArgs e)
{
if (e.PropertyName == "SearchString")
SearchCommand.Execute(null);
};
}
这是正确的吗?在 View 中具有此功能会更好吗?如果是这样,那是如何实现的?
最佳答案
在我看来,更合适的解决方案是从 ViewModel 上的 SearchString 属性的 setter 调用命令。
关于c# - 'call command on property change' 功能应该去哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10413148/