2017-05-23 166 views
-1

我有MainWindow和Window添加,ViewModel和ViewModelADD..l'addition新客戶在數據庫中是正確的,但是DataGrid沒有刷新!當我完成l'addition時,窗口ADD不關閉!從ViewModel中刷新DataGrid fils MVVM WPF

視圖模型:

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

ViewModelADD:

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

    } 

    ViewModel1.Instance.loadDataBinding.Add(customerToAddObject); 

    if (addView != null) 
     addView.Close(); 
    CustomerToAddObject = new Custmor(); 

我嘗試刷新在DataGrid中: ViewModel1.Instance.loadDataBinding.Add(customerToAddObject);

,並試圖關閉該窗口中添加,我嘗試:

if (addView != null) 
    addView.Close(); 
CustomerToAddObject = new Custmor(); 

但總是有問題還是:在DataGrid不刷新和窗口中添加不close..and保存在數據庫中是正確的

+0

我可以猜測的問題是過於寬泛? 「幫我寫我的應用程序」不是一個問題。 你需要更具體。 – Mishka

回答

1

的對象添加到集合,您注入ViewModelADD有:

private readonly ObservableCollection<Custmor> _loadDataBinding; 
public ViewModelADD(ObservableCollection<Custmor> loadDataBinding) 
{ 
    CustomerToAddObject = new Custmor(); 
    addCustomer1 = new RelayCommand(ADDFunction); 
    _loadDataBinding = loadDataBinding; 
} 

... 


private void ADDFunction(object obj) 
{ 
    using (Test1Entities context = new Test1Entities()) 
    { 
     context.Custmor.Add(customerToAddObject); 
     context.SaveChanges(); 
    } 
    _loadDataBinding.Add(customerToAddObject); 
    ... 
} 

而且因爲你從您的視圖模式中打開窗口中,您不妨也注入窗口:

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(); 
    } 
    _loadDataBinding.Add(customerToAddObject); 
    _window.Close(); 
    ... 
} 
+0

謝謝,我修改了第一部分,比如你的第一個答案,即現在的DataGrid刷新。 但第二部分關閉窗口ADD,我有一個錯誤..名稱'窗口'不存在當前的情況下 – devtunis

+0

再次看我的答案。 _window字段在第二個代碼片段的第一行中定義。當您在add()方法中創建視圖模型時,您將引用傳遞給窗口:addView.DataContext = new ViewModelADD(addView,loadDataBinding); – mm8

+1

哦對不起,我不集中.. 是的!它現在有效! 你是天才,;非常感謝! 再次感謝 – devtunis