none
如何将Dictionary`2绑定到DataGrid并可以编辑? RRS feed

  • 问题

  • 当我尝试编辑时抛出异常,具体如下:
    [2012.3.30 19:49:06] An error has occurred. Details here:
    (System.InvalidOperationException) 此视图不允许“EditItem”。
       在 System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item)
       在 System.Windows.Controls.DataGrid.EditRowItem(Object rowItem)
       在 System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)
       在 System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       在 System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
       在 System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       在 System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       在 System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       在 System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       在 System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       在 System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
       在 System.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs)
       在 System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e)
       在 System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       在 System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       在 System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       在 System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       在 System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       在 System.Windows.Input.InputManager.ProcessStagingArea()
       在 System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       在 System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       在 System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

    XAML代码如下:

    <DataGridTextColumn Header="{StaticResource Friction}" Binding="{Binding Value.Friction}" />

    (DataGrid.IsReadOnly和所有的DataGridTextColumn.IsReadOnly为false,且尝试编辑的项不为只读属性)

    我觉得可能是因为KeyValuePair`2是struct类型所以无法修改。因此有没有办法既可以通过一个ID (string)以O(1)的效率访问一个对象(这个对象里有只读属性ID),同时又可以让它支持数据绑定?

    2012年3月31日 10:51

答案

全部回复