2013-02-22 26 views
5

Mam DataGrid wyświetlanie kilku wierszy i kolumn danych, każdy jest związany z własnym Property. DataGrid nie znajduje się na TabControl ale gdy okno, że żyje jest zamknięty i ponownie pojawia się komunikat o błędzie: 'DeferRefresh' is not allowed during an AddNew or EditItem transaction.TYLKO JEŻELI były zagadnienia walidacji z jednej lub więcej komórkach DataGrid „s.WPF DataGrid nie opuszcza trybu edycji

Na przykład: Nieruchomość zobowiązany do DataGrid „s cell jest double a użytkownik wprowadzi«cześć», oczywiście wtedy WPF's automatyczny czerwony granica jest wyświetlany na całym cell. Teraz, gdyby użytkownik zamknął okno i ponownie go otworzył, wystąpiłby błąd.

Wiem, dlaczego błąd jest zgłaszany, ponieważ cell nie opuszcza "trybu edycji".

Jak mogę rozwiązać ten błąd?

Inne uwagi:

  • Mam spojrzał na this question ale to jest odnoszące się do DataGrid na TabControl (który nie jest mój).
  • Podjęto próbę ograniczenia wprowadzania danych przez użytkownika, aby Cell wymagał double lub zaakceptować tylko double, ale problem polega na tym, że nie ma dobrych sposobów na ograniczenie liczby miejsc dziesiętnych ("."), Które użytkownik można wstawić. Kontroluję dane wprowadzane przez użytkownika przez PreviewTextInput i dopuszczam tylko 0-9 i ".".

Wyjątek Szczegóły (czy to pomaga)

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: 

Będę szczęśliwy, aby zapewnić więcej informacji w razie potrzeby.

+0

Czy używasz nowej instancji Window/datagrid za każdym razem.? – Liju

+0

@Liju Tak, jestem. –

+1

Rozwiązałem podobny problem, wywołując buttonBlah.Focus() wewnątrz zdarzenia zamykania okien, wymuszając na urządzeniu zdarzenia LostFocus. Nie całkiem wiem ... Innym rozwiązaniem jest zamknięcie okna tylko za pomocą Przycisków "Zapisz" i "Anuluj". – Jack

Odpowiedz

5

To jest błąd, który został naprawiony w .NET Framework 4.5+. Niestety, dla tych z nas, którzy utknęli w poprzednich wersjach, błąd nadal stanowi problem. Najlepszym rozwiązaniem, które znalazłem, jest po prostu związanie DataGrid z properties z typem string. W ten sposób dostępna jest pełna kontrola nad wprowadzaniem danych przez użytkownika. Przy pełnej kontroli użytkownik nie powinien być w stanie wyzwolić żadnych błędów, a program DataGrid nie spowoduje awarii programu.

+0

Mam dokładnie ten problem z .NET 4.5.1. – l33t

+0

@ l33t Czy kiedykolwiek to naprawiłeś? Również używam .NET 4.5 i nadal to widzę. – MarqueIV

+2

@Jordan, czy możesz utworzyć link do dokumentu pokazującego, kiedy dokładnie zostało to naprawione/zaadresowane? Czy było to w 4.5 lub 4.5.1, 4.5.2 itd.? Nie mogłem znaleźć niczego na temat wyszukiwań, że nawet adresowano go, nie mówiąc już o ustaleniu. – MarqueIV

8

Można próbować wymusić DataGrid do anulowania edycji, kiedy trzeba go:

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

ten pracował dla mnie, miałem podobny problem, gdy zmiana kolejności wierszy.

+1

Gdzie umieścić ten kod? Próbowałem umieścić go na wydarzenie podniesione przez przycisk używany zamknąć okno, ale to nie rozwiązuje problemu dla mnie – FrancescoDS

+0

@FrancescoDS Hej miałem podobny problem, a błąd był wywoływany przez dataGrid.ItemsSource = data; linia. Dosłownie umieściłem to tuż przed linią, która była powodując błąd i zadziałało to dla mnie - dla mnie było to spowodowane błędnym dwukrotnym kliknięciem komórki w datagrid (podwójne kliknięcie powodowało, że program uwierzył, że użytkownik próbował edytować zawartość komórki, ale to nie było zamiar użytkowników) – ajivani