뷰 모델에서 모델 클래스를 올바르게 사용했는지 알고 싶습니다. MVVM으로 Caliburn Micro를 사용합니다.WPF MVVM - 뷰 모델 클래스의 모델 사용
첫 번째 대안입니다.
모델 클래스 :
[Export(typeof(ILogOnViewModel))]
public class LogOnViewModel : Screen
{
public CurrentUser CurrentUser { get; set; }
//bind on control in view
public string CurrentNick
{
get { return CurrentUser.Nick; }
set
{
CurrentUser.Nick = value;
NotifyOfPropertyChange(() => CurrentNick);
}
}
//bind on control in view
public string CurrentPassword
{
get { return CurrentUser.Password; }
set
{
CurrentUser.Password = value;
NotifyOfPropertyChange(() => CurrentPassword);
}
}
}
두 번째 대안 :
모델 클래스 :
public class CurrentUser : IDataErrorInfo, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string Nick
{
get { return _nick; }
set
{
_nick = value;
NotifyPropertyChanged("Nick");
}
}
public string Password
{
get { return _password; }
set
{
_password = value;
NotifyPropertyChanged("Password");
}
}
//...
}
뷰 모델 클래스 모델을 사용
public class CurrentUser : IDataErrorInfo
{
public string Nick { get; set; }
public string Password { get; set; }
//...
}
뷰 모델 클래스에
사용 모델 클래스 : 더 나은보기에서 해당 모델을 캡슐화하기 때문에
이[Export(typeof(ILogOnViewModel))]
public class LogOnViewModel : Screen
{
//bind on UI control
public CurrentUser CurrentUser { get; set; }
}
딥 속성 바인딩을 사용하여 첫 번째 방법을 상당히 단축 할 수 있습니다. 즉, 'CurrentUser' "CurentUser_Nick"(CurrentUser_Password에 대해 동일)의 'Nick'문자열에 바인딩하는 요소의 이름을 지정합니다. 그러면 'CurrentNick'및 'CurrentPassword'속성은 필요하지 않습니다 (NotifyOfPropertyChange를 CurrentUser에 추가합니다.) – EluciusFTW