我正在尝试在我的 WPF 项目上实现 MVVM 模式,该项目使用实体框架来获取/操作数据。我很困惑,我想知道验证我的模型对象集合到数据库的更改应该在哪里???我的应用程序如下:在我看来,我有一个人员数据网格,两个文本框将加载所选人员的姓名/姓氏,一个更新行更改的按钮和一个删除所选行的按钮。
在我的 ModelView 中,我有一个 observableCollection,它将在类的初始化时加载,其中包含来自我的数据库(实体)的信息 + 两个用于添加/删除按钮的 Relaycommands(请在下面找到代码)。
问题是我没有很好地理解 MVVM 的理念,我的数据的修改应该在何时何地以及如何推送到数据库?现在,当我更新我的数据库中的一行,并将我的数据库上下文修改保存在我的 observable 集合上时,提交,但是当我删除一个项目时它不是 cdase,我必须在数据库中手动查找它并删除它(我已将我的 observablecollection 附加到将处理此问题的 NotifyCollectionChangedEventHandler 事件)...我不明白使用 Observable 集合的意义!
有没有使用数据库数据的“完美”mvvm架构的简单解释???谢谢!我的视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
using MVVMOK.Models;
using MVVMOK.Base;
using System.Collections.Specialized;
namespace MVVMOK.ViewModel
{
class MainWindowViewModel : ViewModelBase
{
private DW_MargoEntities contexte;
//Constructor
public MainWindowViewModel()
{
contexte = new DATABASEEntities();
collectionOfCollaborators = new ObservableCollection<Collaborator>();
foreach (Collaborator c in contexte.Collaborator)
{
collectionOfCollaborators.Add(c);
}
//Abonnement pour l'ajout ou la suppression d'éléments :
collectionOfCollaborators.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collectionOfCollaboratorsChanged);
//Assignation des commandes :
this._deleteComand = new RelayCommand(new Action<object>(DeleteRow));
this._updateCommand = new RelayCommand(new Action<object>(UpdateDB));
}
//liste des propriétés publiques:
//Propriété pour représenter l'élément séléctionné du datagrid
private Collaborator selectedItem;
public Collaborator _selectedItem
{
get { return selectedItem; }
set
{
if (value != selectedItem)
{
selectedItem = value;
OnPropertyChanged("_selectedItem");
};
}
}
//Propriété pour représenter l'élément séléctionné:
private ObservableCollection<Collaborator> collectionOfCollaborators;
public ObservableCollection<Collaborator> _collectionOfCollaborators
{
get { return collectionOfCollaborators; }
set
{
this.collectionOfCollaborators = value;
OnPropertyChanged("_collectionOfCollaborators");
}
}
//Commandes :
public ICommand _updateCommand
{
get;
set;
}
public ICommand _deleteComand
{
get;
set;
}
void collectionOfCollaboratorsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Collaborator f = new Collaborator();
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
for(int i = 0; i<e.NewItems.Count;i++)
{
if (e.NewItems[i].GetType().Equals(f.GetType()))
{
contexte.Collaborator.Add(e.NewItems[i] as Collaborator);
}
}
contexte.SaveChanges();
break;
case NotifyCollectionChangedAction.Remove:
for (int i = 0; i < e.OldItems.Count; i++)
{
if (e.OldItems[i].GetType().Equals(f.GetType()))
{
contexte.Collaborator.Remove(e.OldItems[i] as Collaborator);
}
}
contexte.SaveChanges();
break;
//Reset = Clear
}
}
//Services :
public void UpdateDB(object msg)
{
contexte.SaveChanges();
}
public void DeleteRow(object msg)
{
_collectionOfCollaborators.Remove(_selectedItem);
contexte.SaveChanges();
}
}
}
我的模型
namespace MVVMOK.Models
{
using System;
using System.Collections.Generic;
public partial class Collaborator
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}
}
我的 XAML
<Window x:Class="MVVMOK.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMOK.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<!-- Declaratively create an instance of our SongViewModel -->
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid Height="237" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Width="479">
<DataGrid AutoGenerateColumns="False" Height="237" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="479" ItemsSource="{Binding Path=_collectionOfCollaborators, Mode=TwoWay}" SelectionMode="Single" SelectedItem="{Binding Path=_selectedItem, Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Id}" Header="ID" />
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="4*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="104,255,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text ="{Binding Path=_selectedItem.Name, Mode=TwoWay}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="104,283,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text ="{Binding Path=_selectedItem.Surname, Mode=TwoWay}" />
<Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="50,255,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Surname" Height="28" HorizontalAlignment="Left" Margin="37,283,0,0" Name="label2" VerticalAlignment="Top" />
<Button Content="Delete Row" Height="23" HorizontalAlignment="Left" Margin="416,260,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding Path=_deleteComand}"/>
<Button Content="Update All" Height="23" HorizontalAlignment="Left" Margin="335,260,0,0" Name="button2" VerticalAlignment="Top" Width="75" Command="{Binding Path=_updateCommand}"/>
</Grid>
</Window>