2011-12-30 8 views
5

Tôi có một sinh viên treeview. Các mục của tôi trong treeview là tên của học sinh trong một lớp học. Khi lựa chọn một mục trong TreeView, dữ liệu trong DataGrid 'StudentDetails' của tôi sẽ thay đổi. DataGrid của tôi có hai cột 'Parameter_Details' và 'Details'. Bạn có thể chỉnh sửa chi tiết cột thứ hai. Hàng có thể khác nhau tùy thuộc vào việc lựa chọn TreeViewItem.'EditItem' không được phép cho chế độ xem này - Vấn đề chỉnh sửa dữ liệu WPF

Ví dụ, TreeView của tôi có hai mục, nói
Jack
Jill

Khi tôi nhấp/chọn Jack, DataGrid 'Chi tiết Sinh viên' của tôi hiển thị 1 hàng với hai cột String 'Địa chỉ' dưới ' Cột Parameter_Details 'và chuỗi.Empty trong cột' Chi tiết '

Địa chỉ |

Khi tôi nhấp/chọn Jill, DataGrid của tôi 'StudentDetails' show 2 hàng với hai cột 1st hàng: String 'Họ' dưới cột 'Parameter_Details' và string.Empty dưới cột 'Chi tiết' thứ 2 liên tiếp: Chuỗi 'Địa chỉ' trong cột 'Parameter_Details' và chuỗi.Empty trong cột 'Chi tiết' Họ | Địa chỉ |

Tôi đang thêm dữ liệu vào thẻ dữ liệu của mình bằng cách sử dụng vòng lặp foreach. Tôi nhận được 'ParameterDetails' từ DB nơi mỗi hàng đã Parameter tương ứng với TreeViewItem

Bất cứ khi nào tôi chọn TreeViewItem, tôi làm mảnh kèm theo mã:

dgStudentDetails.Items.Clear(); 

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

Các dữ liệu được binded đến DataGrid nhưng khi tôi đang cố gắng để sửa cột thứ 2 sử dụng

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; 
        } 
      } 
     } 
    } 
} 

tôi nhận được lỗi sau:

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: 

Trả lời

4

Tôi cũng gặp lỗi này. Có vẻ như có nhiều nguyên nhân có thể có cùng thông báo lỗi.

Tôi nghĩ trong trường hợp của bạn, nó có thể hoạt động để làm những gì tôi đã làm. Tôi nghĩ rằng vấn đề là DataGrid sẽ ném lỗi này khi chỉnh sửa dữ liệu nếu dữ liệu bị ràng buộc không phải là một trong các loại mà nó sẽ cho phép chỉnh sửa theo.

Điều gì có vẻ phù hợp với tôi, là nếu tôi tạo danh sách mới cho bất kỳ thứ gì tôi muốn hiển thị (có thể là lớp tôi tạo cho mục đích đó, nơi tôi xác định mỗi cột trong lưới) và sau đó liên kết DataGrid vào danh sách đó.

Bạn có thể thấy tôi Q & Một về vấn đề này tại địa chỉ: Why does one of MY WPF DataGrids give the "'EditItem' is not allowed for this view" exception?