简体   繁体   English

WPF MVVM如何将属性绑定到更改上下文查询的comboBox?

[英]WPF MVVM How can i bind a property to comboBox which changes context query?

I have an WPF view with one Combobox and one DataGrid. 我有一个带有一个组合框和一个DataGrid的WPF视图。 I use Entity Framework database first context as a log-term context in my app. 我在应用程序中使用实体框架数据库优先上下文作为日志术语上下文。 Let's say, this context wold be Global.DbContext . 假设此上下文为Global.DbContext My entites created by EF are: Log and Client . EF创建的我的实体是: LogClient In my XAML i have such bindigs: 在我的XAML中,我有这样的绑定:

<DataGrid ItemsSource = {Binding LogEntries} />
<ComboBox ItemsSource="{Binding Clients}" SelectedItem = {Binding SelectedClient} DisplayMemberPath="fullDomainName"
            IsSynchronizedWithCurrentItem="True"/>

In my view model i have these properties (i use Catel Framework, so the properties look a bit strange): 在我的视图模型中,我具有以下属性(我使用Catel Framework,因此这些属性看起来有些奇怪):

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);

and a constructor: 和一个构造函数:

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);
    }

which is not working, because at the time of constructor execution a SelectedClient is null. 这是行不通的,因为在执行构造函数时, SelectedClient为null。 I want my dbset to contain LogEntries only by selected client (both of clients and logs tables in db have a client_id field). 我希望我的数据库集仅包含选定客户机的LogEntries(客户机和db中的日志表都具有一个client_id字段)。 How can i achieve that? 我该如何实现? I undestand that my constructor code is completely wrong, but i can't figure out what to do in context of 'pure MVVM' approach. 我无法理解我的构造函数代码是完全错误的,但是我无法弄清楚在“纯MVVM”方法下该怎么做。 Please help me if you can. 如果可以,请你帮助我。

If I get you right this is the way I would do it: 如果我答对了,这就是我要做的方法:

1) Create new instances of the ObserableCollections in the constructor so the binding will be established. 1)在构造函数中创建ObserableCollections的新实例,以便建立绑定。

2) move the code to fill the LogEntry List to the Setter of SelectedClient 2)移动代码以将LogEntry列表填充到SelectedClient的设置器中

 public Client SelectedClient
{
    get { return GetValue<Client>(SelectedClientProperty); }
    set { 
             SetValue(SelectedClientProperty, value);
             if(value == null)
             {
                 return;
             }
             var qry = Global.DbContext.Logs.Where(c => c.client_id ==   value.client_id);
            qry.Load();
            LogEntries.Clear();
            foreach(var entry in qry)
            {
                LogEntries.Add(entry);
            }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM