Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-WPF/WP从数据库绑定-正确的方法?MVVM_C#_Wpf_Mvvm_Binding_Observablecollection - Fatal编程技术网

C#-WPF/WP从数据库绑定-正确的方法?MVVM

C#-WPF/WP从数据库绑定-正确的方法?MVVM,c#,wpf,mvvm,binding,observablecollection,C#,Wpf,Mvvm,Binding,Observablecollection,假设我将这个类作为一个模型(由它生成的数据库): 在视图模型中,我有一个ObservableCollection Customers,它保存了数据库中的所有客户: ObservableCollection<Customer> Customers; 通过ObservableCollection客户跟踪变更 例如,如果我想在数据库中插入一些内容,也可以将其插入到客户中,UI将收到有关更改的通知 还有什么其他选择? 实现以下目标的最佳MVVM方法是什么: 解析JSON数据并将其插入本地

假设我将这个类作为一个模型(由它生成的数据库):

视图模型中,我有一个ObservableCollection Customers,它保存了数据库中的所有客户:

ObservableCollection<Customer> Customers;
通过ObservableCollection客户跟踪变更

例如,如果我想在数据库中插入一些内容,也可以将其插入到客户中,UI将收到有关更改的通知

还有什么其他选择?

实现以下目标的最佳MVVM方法是什么:

  • 解析JSON数据并将其插入本地数据库
  • 从本地数据库检索数据并用其填充ListBox/ListView
  • 如果将新数据插入数据库或删除/更改项目,请更新ListBox/ListView中的更改

若我想在数据库中添加项目,我可以简单地将项目添加到客户列表并将其添加到数据库中。然而,假设我从数据库中删除了一个对象,可观察的集合“客户”将不会收到通知,并且将不同步(UI也不会更新)。有没有办法深入挖掘MVVM结构,而不是在observable collection和数据库中添加/删除/更改项,以某种方式使observable collection跟踪数据库中的更改(例如,如果更改是通过其他应用程序完成的)。

嗯,我不知道您是否需要解决方案,但是根据你最后的评论你不满意。。。我将说明我的方法

public class CustomersVm : INotifyPropertyChange
{ // just to point out, that you're using a VM class here   
   private ObservableCollection _customers;
   public ObservableCollection Customers
   { // since you're binding this to your listbox, DON'T change the reference during runtime
       get { return _customers ?? (_customers = new ObservableCollection());
   }

   // here comes your loading logic
   public void RefreshCustomers()
   {
       var customers = LoadCustomersFromDb(); // contains now a result set of model classes

       Customers.Clear(); //Clear the collection instead of creating it again.
       foreach(var customer in customers)
           Customers.Add(customer);
   }
}
现在是XAML中的用法,如果
窗口
DataContext
设置为
CustomersVm
的实例,该用法就会起作用。(注意:为了更一般,父元素的
DataContext
需要这样设置。)



我认为您在理解如何用MVVM项目中的项目填充列表框时可能会遇到一些问题。这应该通过绑定完成,而不是直接引用ViewModel中的ListBox。如果您的集合是Customers,则ListBox声明应如下所示:。然后对集合使用Add/Remove/Clear方法来管理其内容。如果添加项目,请将其添加到可观察集合中。如果删除它们,请将其从集合中删除。通过xaml中的绑定而不是代码隐藏来实现这一点。如果您没有使用绑定,那么在WPF中执行MVVM是错误的<代码>更新线程以防止混淆。我想你不明白我的问题。若我想在数据库中添加项目,我可以简单地将项目添加到客户列表并将其添加到数据库中。然而,假设我从数据库中删除了一个对象,可观察的集合“客户”将不会收到通知,并且将不同步(UI也不会更新)。是否有一种方法可以深入挖掘MVVM结构,而不是在observable collection和database中添加/删除/更改项,以某种方式使observable collection跟踪数据库中的更改(例如,如果更改是通过其他应用程序完成的)@Millkovac你找到什么最佳实践了吗?我对我找到的解决方案不是很满意,但我改变了。在ViewModel中,我有一个ObservableCollection,当应用程序运行时,它会从数据库中加载项。然后,我使用绑定将ListBox的ItemsSource属性设置为该ObservableCollection。无论何时我从数据库中添加/删除某些内容,它都会在ObservableCollection中更改,而不是现在只向数据库中删除/添加项。这样,每当数据库中发生更改时,UI都会收到通知,因为这些更改也会反映在ObservableCollection中。
<ListBox ItemsSource="{Binding Customers}" />
ListBoxExample.ItemsSource = null;
ListBoxExample.ItemsSource = Customers;
public class CustomersVm : INotifyPropertyChange
{ // just to point out, that you're using a VM class here   
   private ObservableCollection _customers;
   public ObservableCollection Customers
   { // since you're binding this to your listbox, DON'T change the reference during runtime
       get { return _customers ?? (_customers = new ObservableCollection());
   }

   // here comes your loading logic
   public void RefreshCustomers()
   {
       var customers = LoadCustomersFromDb(); // contains now a result set of model classes

       Customers.Clear(); //Clear the collection instead of creating it again.
       foreach(var customer in customers)
           Customers.Add(customer);
   }
}
<Window DataContext=... >
    <ListBox ItemsSource={Binding Customers} />
</Window>