积极答复者
如何将Dictionary`2绑定到DataGrid并可以编辑?

问题
-
当我尝试编辑时抛出异常,具体如下:
[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),同时又可以让它支持数据绑定?
答案
全部回复
-
我同事Min Zhu在这个帖子中解释了为什么Dictionary绑定到DataGrid后不支持自动生成列,http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dbeb36b2-83aa-43b3-9d59-1e51308480f1/ 你的问题和这个类似,主要是由于Dictionary<TKey,TValue>所实现的接口有关。
参考Dr.WPF的解决方案,来构建一个Observable Dictionary Class: http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
我已经将ObservableDictionaryMaterials类实现了IEditableCollectionView,Materials类改为实现ObservableDictionary<string, Material>,Material类实现了IEditableObject,仍然提示同样的出错信息,如何解决?
- 已编辑 Mygod Studio 2012年4月1日 13:03
-