2011-12-30 6 views
5

Ho una vista sugli studenti. I miei articoli in treeview sono nomi di studenti in una classe. Alla selezione di un oggetto in TreeView, i dati nel mio DataGrid 'StudentDetails' dovrebbero cambiare. My DataGrid ha due colonne 'Parameter_Details' e 'Dettagli'. I dettagli della seconda colonna dovrebbero essere modificabili. Le righe possono variare in base alla selezione di TreeViewItem.'EditItem' non è consentito per questa vista: problema di modifica dei dati WPF

Per esempio, il mio TreeView ha due elementi, dire
Jack
Jill

Quando clicco/selezionare Jack, il mio DataGrid 'Student' dettagli' spettacoli 1 riga con due colonne String 'indirizzo' in' Colonna Parameter_Details 'e string.Empty in colonne' Dettagli '

Indirizzo |

Quando clicco/selezionare Jill, mostra il mio DataGrid 'StudentDetails' 2 file con due colonne 1 ° fila: String 'Cognome' sotto la colonna '' Parameter_Details e string.Empty sotto 'Dettagli' colonne 2 ° fila: String "Indirizzo" nella colonna "Parameter_Details" e stringa.Empty nelle colonne "Dettagli" Cognome | Indirizzo |

Aggiungo dati al mio datagrid utilizzando il ciclo foreach. Ottengo 'ParameterDetails' dal DB in cui ogni riga è parametro corrispondente alla TreeViewItem

Ogni volta che seleziono il TreeViewItem, faccio il pezzo attaccato di codice:

dgStudentDetails.Items.Clear(); 

foreach (Parameter_Details entry in ParameterDetails) 
{ 
    if(entry.ID == SelectedTVItem.ID) 
    { 
     dgKeywordParameters.Items.Add(new Parameter_dgInput() { name = entry.Name, input = "" }); 
    } 
} 

i dati vengono binded al DataGrid ma quando ho sto cercando di modificare 2 ° colonna utilizzando

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    DataGridCell cell = sender as DataGridCell; 

    if (cell != null && !cell.IsEditing && !cell.IsReadOnly) 
    { 
     // enables editing on single click 
     if (!cell.IsFocused) 
     cell.Focus(); 

     DataGrid dataGrid = UIHelpers.TryFindParent<DataGrid>(cell); 

     if (dataGrid != null) 
     { 
      if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) 
      { 
       if (!cell.IsSelected) 
         cell.IsSelected = true; 
      } 
      else 
      { 
        DataGridRow row = UIHelpers.TryFindParent<DataGridRow>(cell); 
        if (row != null && !row.IsSelected) 
        { 
         row.IsSelected = true; 
        } 
      } 
     } 
    } 
} 

ottengo il seguente errore:

System.InvalidOperationException was unhandled 
    Message='EditItem' is not allowed for this view. 
    Source=PresentationFramework 
    StackTrace: 
     at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) 
     at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem) 
     at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) 
     at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender, ExecutedRoutedEventArgs e) 
     at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e) 
     at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding) 
     at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute) 
     at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute) 
     at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e) 
     at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e) 
     at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target) 
     at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
     at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
     at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
     at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
     at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) 
     at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated) 
     at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target) 
     at System.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs) 
     at System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e) 
     at System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e) 
     at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
     at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
     at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
     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.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e) 
     at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
     at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
     at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
     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.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) 
     at System.Windows.Input.InputManager.ProcessStagingArea() 
     at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) 
     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.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.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
     at System.Windows.Application.RunDispatcher(Object ignore) 
     at System.Windows.Application.RunInternal(Window window) 
     at System.Windows.Application.Run(Window window) 
     at System.Windows.Application.Run() 
     at EBS.App.Main() in C:\projects\EBS\EBS\obj\x86\Debug\App.g.cs:line 0 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     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: 

risposta

4

Ho avuto anche questo errore. Sembra che ci siano molte possibili cause per lo stesso messaggio di errore.

Penso che nel tuo caso possa funzionare per fare quello che ho fatto. Penso che il problema è che DataGrid genererà questo errore durante la modifica dei dati se i dati associati non sono uno dei tipi su cui consentirà la modifica.

Ciò che sembra funzionare bene per me, è se creo una nuova lista di ciò che voglio mostrare (forse una classe che creo per quello scopo, dove definisco ogni colonna nella griglia), quindi leghiamo il DataGrid a quella lista.

Potete vedere il mio Q & A in merito a questo a: Why does one of MY WPF DataGrids give the "'EditItem' is not allowed for this view" exception?

Problemi correlati