title: Why are my User Settings saved only the first time this method is called?
body: <p>I have a WPF MVVM app, which gets its data from a user setting which is an <code>ObservableCollection</code> of type <code>Copyable</code> (a custom class) called <code>Copyables</code>. Within the main view model (<code>ClipboardAssistantViewModel</code>), I set the source of a <code>CollectionViewSource</code> to <code>Copyables</code>. This is then bound to an <code>ItemsControl</code> in the main view (<code>MainWindow</code>). The <code>DataTemplate</code> for this <code>ItemsControl</code> is a user control, 'CopyableControl', which is essentially a button, but with attached properties that allow me to bind data and commands to it.</p>
  
  <p>When a user clicks on a <code>CopyableControl</code>, a view model (<code>DefineCopyableViewModel</code>) is added to an <code>ObservableCollection</code> of those in <code>ClipboardAssistantViewModel</code>, and that collection is bound to an <code>ItemsControl</code> in <code>MainWindow</code>. The <code>DataTemplate</code> of this is a <code>UserControl</code> called <code>DefineCopyableControl</code>, which is set up in such a way that the current values associated with the clicked <code>Copyable</code> are bound to textboxes in the <code>DefineCopyableControl</code> for editing.</p>
  
  <p>My problem: There is a method in <code>DefineCopyableViewModel</code>, <code>EditCopyable()</code>, which only works on the first run (its job is to save the user settings once any edits have taken place and the user clicks <em>"OK"</em>). If I click the <code>CopyableControl</code> and make an edit, then click <em>"OK"</em>, then click it again, make another edit, then click <em>"OK"</em>, then close the application and open it again, only the first edit has been saved (even though the UI was updated with the edited value both times). It seems to have something to do with the data-binding need to be "refreshed"; please see the comments in this method in the code for my findings around this.</p>
  
  <p>My code is as follows:</p>
  
  <p>Model:</p>
  
  <pre><code>namespace ClipboardAssistant.Models
  {
      public class Copyable : INotifyPropertyChanged
      {
          // INotifyPropertyChanged implementation
  
          private string name;
          public string Name
          {
              get { return name; }
              set
              {
                  if (value != name)
                  {
                      name = value;
                      NotifyPropertyChanged("Name");
                  }
              }
          }
  
          private string textToCopy;
          public string TextToCopy
          {
              get { return textToCopy; }
              set
              {
                  if (value != textToCopy)
                  {
                      textToCopy = value;
                      NotifyPropertyChanged("TextToCopy");
                  }
              }
          }
  
          public Copyable() { }
  
          public Copyable(string Name, string TextToCopy)
          {
              this.Name = Name;
              this.TextToCopy = TextToCopy;
          }
      }
  }
  </code></pre>
  
  <p>ViewModels:</p>
  
  <pre><code>namespace ClipboardAssistant.ViewModels
  {
      public class ClipboardAssistantViewModel : INotifyPropertyChanged
      {
          // INotifyPropertyChanged Implementation
  
          public CollectionViewSource CopyablesView { get; set; }
  
          public ObservableCollection&lt;DefineCopyableViewModel&gt; Definers { get; set; }
  
          public CopyableClickCommand CopyableClickCommand { get; set; }
  
          public ClipboardAssistantViewModel()
          {
              Definers = new ObservableCollection&lt;DefineCopyableViewModel&gt;();
  
              CopyablesView = new CollectionViewSource();
              CopyablesView.Source = Properties.Settings.Default.Copyables;
  
              CopyableClickCommand = new CopyableClickCommand(this);
              EditModeClickCommand = new EditModeClickCommand(this);
          }
  
          public void RefreshCopyables()
          {
              // Both these methods of refreshing appear to have the same effect.
              Properties.Settings.Default.Copyables = (ObservableCollection&lt;Copyable&gt;)CopyablesView.Source;
              CopyablesView.Source = Properties.Settings.Default.Copyables;
          }
  
          public void EditCopyable(Copyable Copyable)
          {
              Definers.Add(new DefineCopyableViewModel(Copyable, this));
          }
      }
  }
  
  namespace ClipboardAssistant.ViewModels
  {
      public class DefineCopyableViewModel : INotifyPropertyChanged
      {
          // INotifyPropertyChanged Implementation
  
          public ClipboardAssistantViewModel MyParent { get; set; }
  
          public Copyable Copyable { get; set; }
  
          public DefinerOKClickCommand DefinerOKClickCommand { get; set; }
  
          public DefineCopyableViewModel(Copyable Copyable, ClipboardAssistantViewModel MyParent)
          {
              this.Copyable = Copyable;
              this.MyParent = MyParent;
  
              DefinerOKClickCommand = new DefinerOKClickCommand(this);
          }
  
          public void EditCopyable()
          {
              // Refresh, save, no refresh, save -&gt; doesn't save second edit.
  
              // Save, refresh, save, no refresh -&gt; does save second edit.
  
              MessageBoxResult r = MessageBox.Show("Refresh?", "Refresh", MessageBoxButton.YesNo);
              if (r == MessageBoxResult.Yes)
              {
                  MyParent.RefreshCopyables();
              }
  
              // These two MessageBox methods (save and refresh) can be swapped around (see above comments).
  
              MessageBoxResult s = MessageBox.Show("Save?", "Save", MessageBoxButton.YesNo);
              if (s == MessageBoxResult.Yes)
              {
                  Properties.Settings.Default.Save();
              }
  
              MyParent.Definers.Remove(this);
          }
      }
  }
  </code></pre>
  
  <p>MainWindow:</p>
  
  <pre><code>&lt;Window x:Class="ClipboardAssistant.Views.MainWindow" x:Name="mainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                  xmlns:vm="clr-namespace:ClipboardAssistant.ViewModels"
                  xmlns:ctrls="clr-namespace:ClipboardAssistant.Controls"
                  mc:Ignorable="d"
                  Title="Clipboard Assistant" Height="400" Width="700"&gt;
      &lt;Window.DataContext&gt;
          &lt;vm:ClipboardAssistantViewModel /&gt;
      &lt;/Window.DataContext&gt;
  
      &lt;Grid&gt;
          &lt;Grid Margin="15"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="*" /&gt;
                  &lt;RowDefinition Height="20" /&gt;
                  &lt;RowDefinition Height="30" /&gt;
              &lt;/Grid.RowDefinitions&gt;
  
              &lt;ItemsControl ItemsSource="{Binding CopyablesView.View}"&gt;
                  &lt;ItemsControl.ItemsPanel&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;WrapPanel Orientation="Vertical" /&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/ItemsControl.ItemsPanel&gt;
  
                  &lt;ItemsControl.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;ctrls:CopyableControl Copyable="{Binding}"
                                                 ClickCopyable="{Binding DataContext.CopyableClickCommand, ElementName=mainWindow}" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ItemsControl.ItemTemplate&gt;
              &lt;/ItemsControl&gt;
  
              &lt;DockPanel Grid.Row="2"&gt;
                  &lt;Button x:Name="btnEditCopyableMode" HorizontalAlignment="Left" DockPanel.Dock="Left"
                          Content="Edit" Margin="0,0,10,0" Command="{Binding EditModeClickCommand}" /&gt;
              &lt;/DockPanel&gt;
          &lt;/Grid&gt;
  
          &lt;ItemsControl ItemsSource="{Binding Definers}"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;Grid /&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
  
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ctrls:DefineCopyableControl Copyable="{Binding DataContext.Copyable}"
                                                   ClickCancel="{Binding DataContext.DefinerCancelClickCommand, ElementName=mainWindow}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
          &lt;/ItemsControl&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>CopyableControl:</p>
  
  <pre><code>&lt;UserControl x:Class="ClipboardAssistant.Controls.CopyableControl" x:Name="copyableControl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:ClipboardAssistant.Controls"
               mc:Ignorable="d" d:DesignHeight="75" d:DesignWidth="200"&gt;
      &lt;Grid Width="200" Height="75"&gt;
              &lt;Button Command="{Binding ClickCopyable, ElementName=copyableControl}"
                  CommandParameter="{Binding Copyable, ElementName=copyableControl}"
                  Content="{Binding Copyable.Name, ElementName=copyableControl}"
                  Style="{StaticResource CopyableMainButtonStyle}" /&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>namespace ClipboardAssistant.Controls
  {
      public partial class CopyableControl : UserControl
      {
          public static readonly DependencyProperty ClickCopyableProperty =
              DependencyProperty.Register("ClickCopyable", typeof(ICommand), typeof(CopyableControl));
  
          public ICommand ClickCopyable
          {
              get { return (ICommand)GetValue(ClickCopyableProperty); }
              set { SetValue(ClickCopyableProperty, value); }
          }
  
          public static readonly DependencyProperty CopyableProperty =
              DependencyProperty.Register("Copyable", typeof(Copyable), typeof(CopyableControl));
  
          public Copyable Copyable
          {
              get { return (Copyable)GetValue(CopyableProperty); }
              set { SetValue(CopyableProperty, value); }
          }
  
          public CopyableControl()
          {
              InitializeComponent();
          }
      }
  }
  </code></pre>
  
  <p>DefineCopyableControl:</p>
  
  <pre><code>&lt;UserControl x:Class="ClipboardAssistant.Controls.DefineCopyableControl" x:Name="defineCopyableControl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="500"&gt;
      &lt;Grid x:Name="MainGrid" Background="Blue"&gt;
          &lt;Grid Width="200" Height="180"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="30" /&gt;
                  &lt;RowDefinition Height="30" /&gt;
                  &lt;RowDefinition Height="10" /&gt;
                  &lt;RowDefinition Height="30" /&gt;
                  &lt;RowDefinition Height="30" /&gt;
                  &lt;RowDefinition Height="20" /&gt;
                  &lt;RowDefinition Height="30" /&gt;
              &lt;/Grid.RowDefinitions&gt;
  
              &lt;Label Grid.Row="0" Content="Name" Foreground="White" /&gt;
              &lt;TextBox Grid.Row="1" Text="{Binding Copyable.Name}" x:Name="tbN" /&gt;
  
              &lt;Label Grid.Row="3" Content="Copyable Text" Foreground="White" /&gt;
              &lt;TextBox Grid.Row="4" Text="{Binding Copyable.TextToCopy}" x:Name="tbTTC" /&gt;
  
              &lt;DockPanel Grid.Row="6"&gt;
                  &lt;Button Width="70" Content="OK" DockPanel.Dock="Right" HorizontalAlignment="Right"
                          Command="{Binding DefinerOKClickCommand}"
                          CommandParameter="{Binding ElementName=defineCopyableControl}" /&gt;
              &lt;/DockPanel&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>public partial class DefineCopyableControl : UserControl
  {
      public static readonly DependencyProperty CopyableProperty =
          DependencyProperty.Register("Copyable", typeof(Copyable), typeof(DefineCopyableControl));
  
      public Copyable Copyable
      {
          get { return (Copyable)GetValue(CopyableProperty); }
          set { SetValue(CopyableProperty, value); }
      }
  
      public DefineCopyableControl()
      {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p>Commands:</p>
  
  <pre><code>namespace ClipboardAssistant.ViewModels.Commands
  {
      public class CopyableClickCommand : ICommand
      {
          public ClipboardAssistantViewModel ViewModel { get; set; }
  
          public CopyableClickCommand(ClipboardAssistantViewModel viewModel)
          {
              ViewModel = viewModel;
          }
  
          public event EventHandler CanExecuteChanged;
  
          public bool CanExecute(object parameter)
          {
              return true;
          }
  
          public void Execute(object parameter)
          {
              Copyable cp = (Copyable)parameter;
  
              ViewModel.EditCopyable(cp);
          }
      }
  }
  
  namespace ClipboardAssistant.ViewModels.Commands
  {
      public class DefinerOKClickCommand : ICommand
      {
          public DefineCopyableViewModel ViewModel { get; set; }
  
          public DefinerOKClickCommand(DefineCopyableViewModel viewModel)
          {
              ViewModel = viewModel;
          }
  
          public event EventHandler CanExecuteChanged;
  
          public bool CanExecute(object parameter)
          {
              return true;
          }
  
          public void Execute(object parameter)
          {
              ViewModel.EditCopyable();
          }
      }
  }
  </code></pre>
  
  <p>Settings:</p>
  
  <pre><code>namespace ClipboardAssistant.Properties {
  
  
      [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
      [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
      internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
  
          private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
  
          public static Settings Default {
              get {
                  return defaultInstance;
              }
          }
  
          [global::System.Configuration.UserScopedSettingAttribute()]
          [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
          public global::System.Collections.ObjectModel.ObservableCollection&lt;ClipboardAssistant.Models.Copyable&gt; Copyables {
              get {
                  return ((global::System.Collections.ObjectModel.ObservableCollection&lt;ClipboardAssistant.Models.Copyable&gt;)(this["Copyables"]));
              }
              set {
                  this["Copyables"] = value;
              }
          }
      }
  }
  </code></pre>