|
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Linq;
using System.Reflection;
namespace UnsavedDataDetection
{
public class HomeViewModel : INotifyPropertyChanged
{
public HomeViewModel()
{
// Set the command property
SaveCommand = new DelegateCommand(Save, CanSave);
// Wire-up property changed event handler
PropertyChanged += new PropertyChangedEventHandler(HomeViewModel_PropertyChanged);
// Set default values
FullName = "John Doe";
Email = "JohnDoe@Whitehouse.gov";
}
// Commands
#region SaveCommand
public ICommand SaveCommand { get; set; }
public void Save(object param)
{
// Clear IsDirty Flag
// (normally you would actually perform a save first)
// Get an instance of the App class
App AppObj = (App)App.Current;
// Clear all the ISDirty flags
AppObj.objApplicationState.ClearIsDirty();
// Set IsDirty on this class
IsDirty = false;
}
private bool CanSave(object param)
{
// Only enable if form is Dirty
return (IsDirty);
}
#endregion
// Properties
#region IsDirty
private bool _IsDirty;
public bool IsDirty
{
get { return _IsDirty; }
set
{
if (IsDirty == value)
{
return;
}
_IsDirty = value;
this.NotifyPropertyChanged("IsDirty");
}
}
#endregion
#region FullName
private string _FullName;
public string FullName
{
get { return _FullName; }
set
{
if (FullName == value)
{
return;
}
_FullName = value;
this.NotifyPropertyChanged("FullName");
}
}
#endregion
#region Email
private string _Email;
public string Email
{
get { return _Email; }
set
{
if (Email == value)
{
return;
}
_Email = value;
this.NotifyPropertyChanged("Email");
}
}
#endregion
// Events
#region HomeViewModel_PropertyChanged
void HomeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Run this method for any property other than the IsDirty property
// otherwise you will be in in infinate loop
if (e.PropertyName != "IsDirty")
{
// Create a new ApplicationElement
ApplicationElement objApplicationElement = new ApplicationElement();
objApplicationElement.ElementKey = string.Format("HomeViewModel_{0}", e.PropertyName);
objApplicationElement.ElementName = e.PropertyName;
// Set ElementCurrentValue
PropertyInfo pi = this.GetType().GetProperty(e.PropertyName);
objApplicationElement.ElementCurrentValue = Convert.ToString(pi.GetValue(this, null));
// Get an instance of the App class
App AppObj = (App)App.Current;
// Add the ApplicationElement to the objApplicationState object
AppObj.objApplicationState.AddElement(objApplicationElement);
// Set IsDirty
IsDirty = (AppObj.objApplicationState.Elements.Where(x => x.IsDirty == true).Count() > 0);
}
}
#endregion
// Utility
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.