2013-02-22 16 views
5

Ho un DataGrid che visualizza più righe e colonne di dati, ognuna delle quali è vincolata al proprio Property. Il DataGrid NON è su un TabControl ma ogni volta che la finestra su cui si trova viene chiusa e riaperta viene visualizzato il messaggio di errore: 'DeferRefresh' is not allowed during an AddNew or EditItem transaction.SOLO SE ci sono stati problemi di convalida con una o più celle di DataGrid.WPF DataGrid non in uscita in modalità di modifica

Ad esempio: La proprietà legata alla s' il DataGridcell è un double e l'utente immette 'ciao', naturalmente poi WPF's bordo rosso automatica viene visualizzato intorno al cell. Ora, se l'utente dovesse chiudere la finestra e riaprirla, si verificherebbe l'errore.

so perché l'errore viene gettata, perché il cell non lascia "modalità di modifica".

Come posso risolvere questo errore?

Altre note:

  • Ho guardato this question ma che è di pertinenza di un DataGrid su un TabControl (che il mio non è).
  • Ho tentato di limitare l'input dell'utente in modo che se lo Cell richiede un double o accetta solo un double, ma il problema è che non ci sono buoni modi per limitare il numero di punti decimali (".") Che l'utente può inserire. Sto controllando l'input dell'utente tramite PreviewTextInput e consentendo solo 0-9 e ".".

eccezione Particolare (se questo aiuta)

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& 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& handled) 
     at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 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& 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: 

sarò felice di fornire più informazioni, se necessario.

+0

Stai usando new Window/istanza DataGrid ogni volta.? – Liju

+0

@Liju Sì, lo sono. –

+1

ho risolto un problema simile chiamando buttonBlah.Focus() all'interno della manifestazione di chiusura della finestra, questo forza l'evento LostFocus di sparare sulla cella. Non è carina, lo so ... Un'altra soluzione è permettere solo la chiusura della finestra usando i pulsanti "Salva" e "Annulla". – Jack

risposta

5

Si tratta di un bug che è stato fissato con .NET Framework 4.5+. Sfortunatamente, per quelli di noi bloccati con le versioni precedenti, l'errore è ancora un problema. La soluzione migliore che ho trovato è quello di legare semplicemente il DataGrid-properties con il tipo string. In questo modo, è disponibile il pieno controllo sull'input dell'utente. Con il pieno controllo, l'utente non dovrebbe essere in grado di innescare eventuali errori, e il DataGrid non vada in crash il programma.

+0

Ho questo esatto problema con .NET 4.5.1. – l33t

+0

@ l33t Hai mai risolto il problema? Anch'io sto eseguendo .NET 4.5 e sto ancora vedendo questo. – MarqueIV

+2

@Jordan, puoi collegare al documento che mostra quando esattamente questo è stato risolto/indirizzato? Era in 4.5 o 4.5.1, 4.5.2, ecc.? Non sono riuscito a trovare nulla nelle ricerche che è stato anche indirizzato e tanto meno risolto. – MarqueIV

8

Si può provare a forzare datagrid in annullando edizione quando ne avete bisogno:

myDatagrid.CommitEdit(); 
myDatagrid.CancelEdit(); 

Questo ha funzionato per me, ho avuto un problema simile quando riordino righe.

+1

Dove metti quel codice? Ho provato a metterlo sull'evento sollevato dal pulsante usato per chiudere la finestra ma non risolve il problema per me – FrancescoDS

+0

@FrancescoDS Ehi, stavo avendo un problema simile, e l'errore veniva attivato da un oggetto DataGrid.ItemsSource = dati ;.Lineavo letteralmente proprio davanti alla linea che era causando l'errore e ha funzionato per me - per me è stato causato da un doppio clic errato su una cella nel datagrid (il doppio clic stava facendo credere al programma che l'utente stava provando a modificare il contenuto di una cella ma che non era il intenzione degli utenti) – ajivani