2017-04-06 106 views
-2

我目前正在使用MVVM和實體框架代碼優先模型的項目。所以我在我的模型類中實現了IDataErrorInfo和INotifyPropertyChanged。現在,在我的viewmodel中,我實現了一個SaveCommand和一個CanSave布爾方法,我的問題是如何爲整個實體提高canexecutechanged而不是單獨的屬性?因爲我的屬性已經在模型中實現了InotifyPropertyChanged。使用實體框架MVVM驗證

這是我的模型類

public class Guest:ValidatableBindableClass 
{ 
    //my properties here 
    //implement InotifyPropertyChanged and IDataErrorInfo 
} 

這是我ViewModelClass:

public class AddEditGuestViewModel:BindableClass 
{ 
    private Hospede guest; 
    public RelayCommand SaveCommand { get; set; } 
    private readonly Hmsdb.HMS context = new Hmsdb.HMS(); 

    public Hospede Guest 
    { 
     get { return guest; } 
     set { SetProperty(ref guest, value, propertyName: "Guest"); } 
    } 

    private void OnSave() 
    { 

     context.Hospedes.Add(Guest); 
     context.SaveChanges(); 
    } 

    private bool CanSave() 
    { 
     return context.Entry(Guest) 
      .GetValidationResult().IsValid; 
    } 
} 
+1

請勿發佈代碼圖片。將相關代碼複製到相應的問題和格式中。只是發佈圖像會降低問題的可搜索性。 – Lithium

回答

0

你可以通過nullstring.EmptyPropertyChangedEventArgs的構造提高PropertyChanged事件的所有屬性:

PropertyChanged(this, new PropertyChangedEventArgs(null)); 

但是,您需要調用每個RelayCommandRaiseCanExecuteChanged()方法。

+0

根據命令實現,對'CommandManager.InvalidateRequerySuggested()'的調用可能已經足夠了。 – grek40

+0

雖然這是非常無效的:http://stackoverflow.com/questions/1751966/commandmanager-invalidaterequerysuggested-isnt-fast-enough -我能做什麼。無論如何,這個實體似乎只有一個命令。 – mm8

+0

我的模型類已經爲每個屬性實現了屬性changed事件,問題是我怎麼才能提出canexecute改變viewmodel類sinse我綁定到訪客實體,而不是Gues.SomeProperty?感謝您的重播... – Gonguela