简介 我是WPF和mvvm的新手,因此我尝试通过应用库存管理来掌握基础知识。
技术-堆栈
问题说明
Add
或Delete
操作不会自动更新我的datagrid
自动更新。
我在论坛上阅读了很多内容,但无所事事。
代码
ArticleViewModel
Class ArticleViewModel
{
private GStockEntities context;
public ArticleViewModel()
{
context = new GStockEntities();
}
public ObservableCollection<article> GetAllArticle()
{
ObservableCollection<article> Article = new ObservableCollection<article>();
try
{
var query = (from a in context.articles select a).ToList();
foreach (var item in query)
{
Article.Add(item);
}
}
catch (Exception)
{
throw;
}
return Article;
}
public ObservableCollection<article> Article
{
get
{
return this.GetAllArticle();
}
set
{
Set(ref _Article, value);
}
}
private void Add()
{
article Article = new article();
Article.id_article = id;
Article.nom = nom_Article;
Article.id_categorie = GetIdCategoriByName(nom_categorie);
Article.quantite = (int)qt;
Article.prix_achat = (decimal)prix_achat;
Article.prix_vente = (decimal)prix_vente;
context.articles.Add(Article);
context.SaveChanges();
}
}
ViewModelBase
class ViewModelBase : INotifyPropertyChanged, INotifyDataErrorInfo
{
public ViewModelBase()
{
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void Notify([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected bool Set<TProperty>(ref TProperty attrib,
TProperty value, [CallerMemberName]string propertyName = null)
{
if (!object.Equals(attrib, value))
{
OnPropertyChanging(propertyName, attrib, value);
attrib = value;
Notify(propertyName);
OnPropertyChanged(propertyName);
return true;
}
return false;
}
protected virtual void OnPropertyChanged(string propertyName) { }
protected virtual void OnPropertyChanging(string propertyName,
object oldValue, object newValue) { }
#endregion
}
ArticleView
<DataGrid DockPanel.Dock="Right"
AutoGenerateColumns="False"
Name="ArtDatagrid"
ItemsSource="{Binding Article}"
SelectedItem="{Binding SelectedArticle}"
CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn
Header="Id"
Binding="{Binding id_article}"
Width="*"/>
<DataGridTextColumn
Header="Nom"
Binding="{Binding nom}"
Width="*"/>
<DataGridTextColumn
Header="Nom de la categorie"
Binding="{Binding nom_categorie}"
Width="*"/>
<DataGridTextColumn
Header="QT"
Binding="{Binding quantite}"
Width="*"/>
<DataGridTextColumn
Header="Prix d'achat"
Binding="{Binding prix_achat}"
Width="*"/>
<DataGridTextColumn
Header="prix de vente"
Binding="{Binding prix_vente}"
Width="*"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
答案 0 :(得分:1)
因为您将新创建的文章添加到数据库中,而不是添加到绑定到网格的ObservableCollection
。
一种解决方案可能是不直接在getter中从数据库返回文章。相反,您可以使用局部变量来存储它们:
private ObservableCollection<article> _article
public ObservableCollection<article> Article
{
get
{
if (_article == null)
_article = this.GetAllArticle();
return _article;
}
}
然后您可以在add方法中使用该集合:
private void Add()
{
article Article = new article();
Article.id_article = id;
Article.nom = nom_Article;
Article.id_categorie = GetIdCategoriByName(nom_categorie);
Article.quantite = (int)qt;
Article.prix_achat = (decimal)prix_achat;
Article.prix_vente = (decimal)prix_vente;
context.articles.Add(Article);
context.SaveChanges();
_article.Add(Article);
}