【发布时间】:2017-02-15 19:22:41
【问题描述】:
我有一个带有一个 Combobox 和一个 DataGrid 的 WPF 视图。我在我的应用程序中使用 Entity Framework 数据库第一个上下文作为日志项上下文。
假设,这个上下文是Global.DbContext。
我由 EF 创建的实体是:Log 和 Client。
在我的 XAML 中,我有这样的绑定:
<DataGrid ItemsSource = {Binding LogEntries} />
<ComboBox ItemsSource="{Binding Clients}" SelectedItem = {Binding SelectedClient} DisplayMemberPath="fullDomainName"
IsSynchronizedWithCurrentItem="True"/>
在我的视图模型中,我有这些属性(我使用的是 Catel 框架,所以这些属性看起来有点奇怪):
public ObservableCollection<Log> LogEntries
{
get { return GetValue<ObservableCollection<Log>>(LogEntriesProperty); }
set { SetValue(LogEntriesProperty, value); }
}
public static readonly PropertyData LogEntriesProperty = RegisterProperty("LogEntries", typeof(ObservableCollection<Log>), null);
public ObservableCollection<Client> Clients
{
get { return GetValue<ObservableCollection<Client>>(ClientsProperty); }
set { SetValue(ClientsProperty, value); }
}
public static readonly PropertyData ClientsProperty = RegisterProperty("Clients", typeof(ObservableCollection<Client>), null);
public Client SelectedClient
{
get { return GetValue<Client>(SelectedClientProperty); }
set { SetValue(SelectedClientProperty, value); }
}
public static readonly PropertyData SelectedClientProperty = RegisterProperty("SelectedClient", typeof(Client), null);
还有一个构造函数:
public LogWindowViewModel()
{
Global.DbContext.Clients.Load();
Clients = Global.DbContext.Clients.Local;
var qry = Global.DbContext.Logs.Where(c => c.client_id == SelectedClient.client_id);
qry.Load();
LogEntries = new ObservableCollection<Log>(qry);
}
这不起作用,因为在构造函数执行时SelectedClient 为空。我希望我的 dbset 仅包含选定客户端的 LogEntries(db 中的客户端和日志表都有一个 client_id 字段)。我怎样才能做到这一点?
我不明白我的构造函数代码是完全错误的,但我无法弄清楚在“纯 MVVM”方法的上下文中该怎么做。如果可以,请帮助我。
【问题讨论】:
标签: c# wpf entity-framework mvvm