title: MVVM - implementing 'IsDirty' functionality to a ModelView in order to save data
body: <p>Being new to WPF &amp; MVVM I struggling with some basic functionality.</p>
  
  <p>Let me first explain what I am after, and then attach some example code...</p>
  
  <p>I have a screen showing a list of users, and I display the details of the selected user on the right-hand side with editable textboxes.  I then have a Save button which is DataBound, but I would only like this button to display when data has actually changed.  ie - I need to check for "dirty data".</p>
  
  <p>I have a fully MVVM example in which I have a Model called User:</p>
  
  <pre><code>namespace Test.Model
  {
      class User
      {
          public string UserName { get; set; }
          public string Surname { get; set; }
          public string Firstname { get; set; }
      }
  }
  </code></pre>
  
  <p>Then, the ViewModel looks like this:</p>
  
  <pre><code>using System.Collections.ObjectModel;
  using System.Collections.Specialized;
  using System.Windows.Input;
  using Test.Model;
  
  namespace Test.ViewModel
  {
      class UserViewModel : ViewModelBase
      {
          //Private variables
          private ObservableCollection&lt;User&gt; _users;
          RelayCommand _userSave;
  
          //Properties
          public ObservableCollection&lt;User&gt; User
          {
              get
              {
                  if (_users == null)
                  {
                      _users = new ObservableCollection&lt;User&gt;();
                      //I assume I need this Handler, but I am stuggling to implement it successfully
                      //_users.CollectionChanged += HandleChange;
  
                      //Populate with users
                      _users.Add(new User {UserName = "Bob", Firstname="Bob", Surname="Smith"});
                      _users.Add(new User {UserName = "Smob", Firstname="John", Surname="Davy"});
                  }
                  return _users;
              }
          }
  
          //Not sure what to do with this?!?!
  
          //private void HandleChange(object sender, NotifyCollectionChangedEventArgs e)
          //{
          //    if (e.Action == NotifyCollectionChangedAction.Remove)
          //    {
          //        foreach (TestViewModel item in e.NewItems)
          //        {
          //            //Removed items
          //        }
          //    }
          //    else if (e.Action == NotifyCollectionChangedAction.Add)
          //    {
          //        foreach (TestViewModel item in e.NewItems)
          //        {
          //            //Added items
          //        }
          //    } 
          //}
  
          //Commands
          public ICommand UserSave
          {
              get
              {
                  if (_userSave == null)
                  {
                      _userSave = new RelayCommand(param =&gt; this.UserSaveExecute(), param =&gt; this.UserSaveCanExecute);
                  }
                  return _userSave;
              }
          }
  
          void UserSaveExecute()
          {
              //Here I will call my DataAccess to actually save the data
          }
  
          bool UserSaveCanExecute
          {
              get
              {
                  //This is where I would like to know whether the currently selected item has been edited and is thus "dirty"
                  return false;
              }
          }
  
          //constructor
          public UserViewModel()
          {
  
          }
  
      }
  }
  </code></pre>
  
  <p>The "RelayCommand" is just a simple wrapper class, as is the "ViewModelBase".  (I'll attach the latter though just for clarity)</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  
  namespace Test.ViewModel
  {
      public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
      {
          protected ViewModelBase()
          { 
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected virtual void OnPropertyChanged(string propertyName)
          {
              PropertyChangedEventHandler handler = this.PropertyChanged;
              if (handler != null)
              {
                  var e = new PropertyChangedEventArgs(propertyName);
                  handler(this, e);
              }
          }
  
          public void Dispose()
          {
              this.OnDispose();
          }
  
          protected virtual void OnDispose()
          {
          }
      }
  }
  </code></pre>
  
  <p>Finally - the XAML</p>
  
  <pre><code>&lt;Window x:Class="Test.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vm="clr-namespace:Test.ViewModel"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.DataContext&gt;
          &lt;vm:UserViewModel/&gt;
      &lt;/Window.DataContext&gt;
      &lt;Grid&gt;
          &lt;ListBox Height="238" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBox1" VerticalAlignment="Top" 
                   Width="197" ItemsSource="{Binding Path=User}" IsSynchronizedWithCurrentItem="True"&gt;
              &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel&gt;
                          &lt;TextBlock Text="{Binding Path=Firstname}"/&gt;
                          &lt;TextBlock Text="{Binding Path=Surname}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
          &lt;Label Content="Username" Height="28" HorizontalAlignment="Left" Margin="232,16,0,0" Name="label1" VerticalAlignment="Top" /&gt;
          &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="323,21,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=User/UserName}" /&gt;
          &lt;Label Content="Surname" Height="28" HorizontalAlignment="Left" Margin="232,50,0,0" Name="label2" VerticalAlignment="Top" /&gt;
          &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="323,52,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=User/Surname}" /&gt;
          &lt;Label Content="Firstname" Height="28" HorizontalAlignment="Left" Margin="232,84,0,0" Name="label3" VerticalAlignment="Top" /&gt;
          &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="323,86,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" Text="{Binding Path=User/Firstname}" /&gt;
          &lt;Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="368,159,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding Path=UserSave}" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>So basically, when I edit a surname, the Save button should be enabled; and if I undo my edit - well then it should be Disabled again as nothing has changed.</p>
  
  <p>I have seen this in many examples, but have not yet found out how to do it.</p>
  
  <p>Any help would be much appreciated!
  Brendan</p>