“EditItem”在此视图中不允许 - WPF数据编辑问题

5
我有一个名为“Students”的树形视图。我的树形视图中的项目是班级学生的姓名。在选择树形视图中的项目时,我的DataGrid“StudentDetails”中的数据应该改变。我的DataGrid有两列,“Parameter_Details”和“Details”。第二列“Details”应该是可编辑的。行数可能因TreeViewItem的选择而异。
例如,我的TreeView有两个项目,如下所示: Jack Jill
当我单击/选择Jack时,我的DataGrid“Student Details”显示1行,有两列: 在“Parameter_Details”列下的字符串“Address”,在“Details”列下的字符串为空。
地址|
当我单击/选择Jill时,我的DataGrid“StudentDetails”显示2行,有两列: 第一行: 在“Parameter_Details”列下的字符串“Last Name”,在“Details”列下的字符串为空。 第二行: 在“Parameter_Details”列下的字符串“Address”,在“Details”列下的字符串为空。
姓氏| 地址 |
我正在使用foreach循环向我的datagrid添加数据。我从数据库中获取“ParameterDetails”,其中每行都有与TreeViewItem对应的参数。
每当我选择TreeViewItem时,我会执行附加的代码片段:
dgStudentDetails.Items.Clear();

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

数据已绑定到数据网格,但是当我尝试使用以下方法编辑第二列时:
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;
                  }
             }
        }
    }
}

我收到了以下错误信息:
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:

#访问此处以获取3种解决方法# 访问 https://stackoverflow.com/questions/57683346/commitnew-is-not-allowed-for-this-view! 你看过https://stackoverflow.com/questions/57683346/commitnew-is-not-allowed-for-this-view吗? - Prashant Manjule
2个回答

4

我也遇到过这个错误。看起来同样的错误信息可能有很多种可能的原因。

我认为在你的情况下,可以尝试做我所做的事情。我认为问题在于,如果绑定的数据不是DataGrid允许编辑的类型之一,当编辑数据时,DataGrid将抛出此错误。

对我而言,似乎很有效的方法是,如果我创建一个新的列表来显示任何我想要显示的内容(也许是我为此目的制作的类,在其中定义了网格中的每一列),然后将DataGrid绑定到该列表上。

您可以在以下链接中查看我的关于此问题的问答: 为什么我的WPF DataGrid之一会出现“'EditItem' is not allowed for this view”异常?


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接