CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

VM Base

426 Views
Copy Code Show/Hide Line Numbers
public class StoryFrameEditorViewModel : ViewModel
{
    public StoryFrameEditorViewModel()
    {
        _template = Field<StoryFrameTemplate>("Template");
    }
    
    private BackingField<StoryFrameTemplate> _template; 
    public StoryFrameTemplate Template
    {
        get { return _template.Value; }
        set { _template.Value = value;}
    }
}
 
public abstract class ViewModel : INotifyPropertyChanged
{
    private Dictionary<string, object> _data;
    public void RaisePropertyChanged(string property)
 
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
 
    public BackingField<T> Field<T>(string property)
    {
        return new BackingField<T>(() => RaisePropertyChanged(property));
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
}
 
public class BackingField<T>
{
    private readonly string _propertyName;
    private readonly Action _notifyAction;
    private T _value;
    public T Value
    {
        get { return _value; }
        set
        {
            if (!Equals(value, _value))
            {
                _value = value;
                _notifyAction();
            }
        }
    }
 
    public BackingField(Action notifyAction)
    {
        _notifyAction = notifyAction;
    }
}
 
by Glenn Block
  March 24, 2010 @ 2:41am

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate