【发布时间】:2015-09-26 00:33:28
【问题描述】:
我从 DataLayer 类获取数据到 ViewModel,但它没有进入与视图中的列表框绑定的 observableCollection。
public class ViewModel : NotifyUIBase
{
public ViewModel()
{
FindImageCommand = new RelayCommand(FindImage);
}
public ObservableCollection<Image> FindVisualReferences { get; set; }
public RelayCommand FindImageCommand { get; private set; }
private void FindImage()
{
string SearchTerm = this.SearchBox;
var dbFunctions = new DatabaseFunctions();
FindVisualReferences = dbFunctions.FindVisualReferences(SearchTerm);
}
}
我已经测试了FindVisualReferecences(SearchTerm) 方法,它提供了填充 observableCollection 所需的项目。
我还使用 View 测试了数据绑定,并且效果很好。
当我将方法放在构造函数中时, ObservableCollection 会被填充
public ViewModel()
{
var dbFunctions = new DatabaseFunctions();
FindVisualReferences = dbFunctions.FindVisualReferences(SearchTerm);
}
但我需要在用户给出 FindImageCommand 时调用该方法。 我怎样才能使这项工作?说到编码,我真的是个菜鸟。
【问题讨论】:
-
有什么问题?你没有执行 FindImageCommand 吗?
-
如何激活命令?通过按钮或类似的东西?
-
命令执行,我可以将数据放入 ViewModel,但不知何故,它没有到达 ViewModel 中的 ObservableCollection,尽管 FindImage 方法中的 FindVisualReferences 包含我需要的项目ObservableCollection 与视图绑定。
标签: c# wpf mvvm observablecollection