从viewmodel fils MVVM WPF刷新datagrid [英] Refresh datagrid from viewmodel fils MVVM WPF
问题描述
我有MainWindow和Window Add,ViewModel和ViewModelADD..l'添加新客户在数据库中是正确的,但DataGrid的刷新没有!当我完成l'时,Window ADD不会关闭!
ViewModel:
ViewModelADD:
如何修复它?
添加新的Customer ..并刷新DataGrid,以及客户添加时...窗户添加关闭
谢谢,
我尝试过:
< pre> private static ViewModel1 instance = new ViewModel1();
public static ViewModel1 Instance { get { return 实例;
private void add ( object obj)
{
add addView = new 添加();
addView.DataContext = new ViewModelADD(loadDataBinding);
addView.Show();
}
private ObservableCollection< Custmor> _loadDataBinding;
public ObservableCollection< Custmor> loadDataBinding
{
get
{
return _loadDataBinding ;
}
set
{
_loadDataBinding = value 跨度>;
OnPropertyChanged( loadDataBinding);
}
}
< pre> public ViewModelADD(ObservableCollection< Custmor> loadDataBinding)
{
CustomerToAddObject = new Custmor ();
addCustomer1 = new RelayCommand(ADDFunction);
}
私人 ICommand addCustomer1;
public ICommand AddCustomer1
{
get { return addCustomer1; }
}
私有 void ADDFunction( object obj)
{
使用(Test1Entities context = new Test1Entities())
{
context.Custmor.Add(customerToAddObject);
context.SaveChanges();
MessageBox.Show( 客户aétéajoutéavecsuccès!);
}
ViewModel1.Instance.loadDataBinding.Add(customerToAddObject);
我找到了解决方案,
首先我修改了ViewModel:
private void add ( object obj)
{
添加addView = new Add();
addView.DataContext = new ViewModelADD(addView,loadDataBinding);
addView.Show();
}
然后我修改ViewModelADD:
< pre> private readonly Window _window;
private readonly ObservableCollection< Custmor> _loadDataBinding;
public ViewModelADD(Window window,ObservableCollection< Custmor> loadDataBinding)
{
CustomerToAddObject = new Custmor();
addCustomer1 = new RelayCommand(ADDFunction);
_window = window;
_loadDataBinding = loadDataBinding;
}
private void ADDFunction(< span class =code-keyword> object obj)
{
using (Test1Entities context = new Test1Entities())
{
context.Custmor.Add(customerToAddObject);
context.SaveChanges();
MessageBox.Show( 客户aétéajoutéavecsuccès!);
}
_loadDataBinding.Add(customerToAddObject);
_window.Close();
}
现在工作正常,
Hi,
I have MainWindow and Window Add , ViewModel and ViewModelADD..l'addition of new Customer is correct in database, but the refresh of DataGrid no ! and when i finish l'addition , the Window ADD doesn't close !
ViewModel:
ViewModelADD:
How can I fix it?
to add new Customer..and refresh the DataGrid, and when the Customer added..the Window ADD close
Thanks,
What I have tried:
<pre>private static ViewModel1 instance = new ViewModel1();
public static ViewModel1 Instance { get { return instance; } }
private void add(object obj)
{
Add addView = new Add();
addView.DataContext = new ViewModelADD(loadDataBinding);
addView.Show();
}
private ObservableCollection<Custmor> _loadDataBinding;
public ObservableCollection<Custmor> loadDataBinding
{
get
{
return _loadDataBinding;
}
set
{
_loadDataBinding = value;
OnPropertyChanged("loadDataBinding");
}
}
<pre> public ViewModelADD(ObservableCollection<Custmor> loadDataBinding)
{
CustomerToAddObject = new Custmor();
addCustomer1 = new RelayCommand(ADDFunction);
}
private ICommand addCustomer1;
public ICommand AddCustomer1
{
get { return addCustomer1; }
}
private void ADDFunction(object obj)
{
using (Test1Entities context = new Test1Entities())
{
context.Custmor.Add(customerToAddObject);
context.SaveChanges();
MessageBox.Show("Customer a été ajouté avec succès!");
}
ViewModel1.Instance.loadDataBinding.Add(customerToAddObject);
I found the solution, first I modify the ViewModel:
private void add(object obj) { Add addView = new Add(); addView.DataContext = new ViewModelADD(addView,loadDataBinding); addView.Show(); }
then i modify the ViewModelADD:
<pre> private readonly Window _window; private readonly ObservableCollection<Custmor> _loadDataBinding; public ViewModelADD(Window window,ObservableCollection<Custmor> loadDataBinding) { CustomerToAddObject = new Custmor(); addCustomer1 = new RelayCommand(ADDFunction); _window = window; _loadDataBinding = loadDataBinding; } private void ADDFunction(object obj) { using (Test1Entities context = new Test1Entities()) { context.Custmor.Add(customerToAddObject); context.SaveChanges(); MessageBox.Show("Customer a été ajouté avec succès!"); } _loadDataBinding.Add(customerToAddObject); _window.Close(); }
It works correct now,
这篇关于从viewmodel fils MVVM WPF刷新datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!