// Copyright © 2010-2014 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
namespace CefSharp.Wpf.Example.Mvvm
{
public class ViewModelBase : DisposableResource, INotifyPropertyChanged
{
#region INotifyPropertyChanged
private PropertyChangedEventHandler propertyChanged;
///
public event PropertyChangedEventHandler PropertyChanged
{
add { if (!IsDisposed) propertyChanged += value; }
remove { propertyChanged -= value; }
}
///
/// Is called when a property is changed and raises a event
///
///
/// The old value.
/// The new value.
/// The instance containing the event data.
protected virtual void OnPropertyChanged(T oldvalue, T newValue, PropertyChangedEventArgs e)
{
var handlers = propertyChanged;
if (handlers == null)
{
return;
}
handlers(this, e);
}
public static PropertyChangedEventArgs GetArgs(Expression> propertyexpression)
{
if (propertyexpression == null)
{
throw new ArgumentNullException("propertyexpression");
}
var body = propertyexpression.Body as MemberExpression;
if (body == null)
{
throw new ArgumentException("Lambda must return a property.");
}
return new PropertyChangedEventArgs(body.Member.Name);
}
///
/// Sets the specified field and calls if the value changed.
///
///
/// The field.
/// The value.
/// The instance containing the event data.
protected void Set(ref T field, T value, PropertyChangedEventArgs e)
{
var oldvalue = field;
if (EqualityComparer.Default.Equals(oldvalue, value))
{
return;
}
field = value;
OnPropertyChanged(oldvalue, value, e);
}
#endregion
}
}