First, your MVVM construct is ass-backwards. It should be more like this:
public class Model
{
public string Prop1 { get; set; }
public Model()
{
}
public void GetData()
{
try
{
}
catch (Exception ex)
{
}
}
public void SaveData
{
try
{
}
catch (Exception ex)
{
}
}
}
public class ViewModel : INotifyPropertyChanged, IDataErrorInfo
{
private string prop1;
public string Prop1
{
get { return this.prop1; }
set
{
if (value != this.prop1)
{
this.prop1 = value;
this.NotifyPropertyChanged();
}
}
}
public ViewModel()
{
Model model = new Model();
model.GetData();
this.Prop1 = model.prop1;
}
}
public class Window
{
public ViewModel vm { get; set; }
public Window()
{
this.InitializeComponents();
this.DataContext = this;
this.vm = new ViewModel();
}
}
At that point, you can bind to
vm.Prop1
in your XAML.
Here's a handy class that I wrote and use all the time that inherits from both
INotifyPropertyChanged
and
IDataErrorInfo
. I wrote it to be compatible with new and older versions of .Net. This should ease the pain a little of implementing ViewModel classes. Just inherit it in your own objects.
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfCommon
{
public class Notifiable : INotifyPropertyChanged, IDataErrorInfo
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
#if _NET_45_
protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
if (propertyName != "IsModified")
{
this.IsModified = true;
}
}
}
#else
protected virtual void NotifyPropertyChanged(String propertyName = "")
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
if (propertyName != "IsModified")
{
this.IsModified = true;
}
}
}
#endif
#endregion INotifyPropertyChanged
#region IDataErrorInfo Code
public string Error
{
get { return "Error"; }
}
public string this[string columnName]
{
get
{
return Validate(columnName);
}
}
protected virtual string Validate(string propertyName)
{
string validationMsg = string.Empty;
return validationMsg;
}
#endregion IDataErrorInfo Code
#region Fields
private bool isModified;
private bool isSelected;
private bool isVisible;
private bool isEnabled;
private bool isNew;
private bool isClone;
private bool isDeleted;
private bool isValid;
#endregion Fields
#region Properties
public virtual bool IsModified
{
get { return this.isModified; }
set
{
if (this.isModified != value)
{
this.isModified = value;
this.NotifyPropertyChanged("IsModified");
}
}
}
public virtual bool IsSelected
{
get { return this.isSelected; }
set
{
if (this.isSelected != value)
{
this.isSelected = value;
this.NotifyPropertyChanged("IsSelected");
}
}
}
public virtual bool IsVisible
{
get { return this.isVisible; }
set
{
if (value != this.isVisible)
{
this.isVisible = value;
this.NotifyPropertyChanged("IsVisible");
}
}
}
public virtual bool IsEnabled
{
get { return this.isEnabled; }
set
{
if (value != this.isEnabled)
{
this.isEnabled = value;
this.NotifyPropertyChanged("IsEnabled");
}
}
}
public virtual bool IsNew
{
get { return this.isNew; }
set
{
if (value != this.isNew)
{
this.isNew = value;
this.NotifyPropertyChanged("IsNew");
}
}
}
public virtual bool IsClone
{
get { return this.isClone; }
set
{
if (value != this.isClone)
{
this.isClone = value;
this.NotifyPropertyChanged("IsClone");
}
}
}
public virtual bool IsDeleted
{
get { return this.isDeleted; }
set
{
if (value != this.isDeleted)
{
this.isDeleted = value;
this.NotifyPropertyChanged("IsDeleted");
}
}
}
public virtual bool IsValid
{
get { return this.isValid; }
set
{
if (value != this.isValid)
{
this.isValid = value;
this.NotifyPropertyChanged("IsValid");
}
}
}
#endregion Properties
#region Constructor
public Notifiable(bool isNew=false)
{
this.IsNew = isNew;
this.IsModified = false;
this.IsVisible = true;
this.IsEnabled = true;
this.isValid = true;
this.IsClone = false;
this.IsDeleted = false;
}
#endregion Constructor
}
}