title: WPF DataGrid not exiting edit mode
body: <p>I have a <code>DataGrid</code> displaying several rows and columns of data, each is bound to its own <code>Property</code>. The <code>DataGrid</code> is NOT on a <code>TabControl</code> but whenever the window that it lives on is closed and reopened I get the error message: <code>'DeferRefresh' is not allowed during an AddNew or EditItem transaction.</code> <em>ONLY IF</em> there were validations issues with one or more of the <code>DataGrid</code>'s cells.</p>
  
  <p><strong>For example:</strong> The property bound to the <code>DataGrid</code>'s <code>cell</code> is a <code>double</code> and the user enters "hello", of course then <code>WPF's</code> automatic red border is displayed around the <code>cell</code>. Now, if the user were to close the window, and reopen it the error would occur. </p>
  
  <p><em>I know why</em> the error is being thrown, because the <code>cell</code> is not leaving "edit mode". </p>
  
  <p><strong>How can I resolve this error?</strong></p>
  
  <p><strong>Other Notes:</strong></p>
  
  <ul>
  <li>I have looked at <a href="https://stackoverflow.com/questions/3354916/what-is-the-proper-way-to-handle-multiple-datagrids-in-a-tab-control-so-that-cel">this question</a> but that is pertaining to a <code>DataGrid</code> on a <code>TabControl</code> (which mine is not).</li>
  <li>I have attempted to limit the user's input so that if the <code>Cell</code> requires a <code>double</code> or then only accept a <code>double</code>, but the problem is there are no good ways to limit the number of decimal points (".") that the user can insert. I am controlling user input through <code>PreviewTextInput</code> and only allowing 0-9 and ".". </li>
  </ul>
  
  <hr>
  
  <p><strong>Exception Detail</strong> (if this helps)</p>
  
  <pre><code>System.InvalidOperationException was unhandled
    Message='DeferRefresh' is not allowed during an AddNew or EditItem transaction.
    Source=PresentationFramework
    StackTrace:
         at System.Windows.Data.CollectionView.DeferRefresh()
         at System.Windows.Controls.ItemCollection.SetCollectionView(CollectionView view)
         at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
         at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
         at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
         at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
         at System.Windows.Data.BindingExpression.Activate(Object item)
         at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
         at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
         at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
         at MS.Internal.Data.DataBindEngine.Run(Object arg)
         at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
         at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
         at System.Windows.ContextLayoutManager.UpdateLayout()
         at System.Windows.Interop.HwndSource.SetLayoutSize()
         at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
         at System.Windows.Window.SetRootVisualAndUpdateSTC()
         at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
         at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
         at System.Windows.Window.ShowHelper(Object booleanBox)
         at REACT.ViewModel.ReceiverListViewModel.ShowWindow(String name) in C:\Users\jcarroll42\Documents\Sandbox\REACT\SW\Source\ViewModel\ReceiverListViewModel.cs:line 238
         at REACT.Commands.ShowWindowCommand.Execute(Object parameter) in C:\Users\jcarroll42\Documents\Sandbox\REACT\SW\Source\Commands.cs:line 137
         at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
         at System.Windows.Controls.Button.OnClick()
         at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
         at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
         at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
         at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
         at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
         at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
         at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
         at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
         at System.Windows.Input.InputManager.ProcessStagingArea()
         at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
         at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
         at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
         at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
         at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
         at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
         at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
         at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
         at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)
         at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
         at System.Windows.Application.RunInternal(Window window)
         at System.Windows.Application.Run()
         at REACT.App.Main() in C:\Users\jcarroll42\Documents\Sandbox\REACT\SW\Source\obj\x86\Debug\App.g.cs:line 0
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: 
  </code></pre>
  
  <p>I'll be happy to provide anymore information if needed.</p>