Crash when binding to SelectedItem in DataGrid

Apr 17, 2009 at 3:08 AM
Hi,
I am using the DataGrid with an MVVM design. So, on my ViewModel class (which is set as the DataContext of my view), I have a "CurrentSelection" property. In the XAML for the DataGrid, I have a binding for the selected item to my property. Something like:
<WPFToolkt:DataGrid
    ItemsSource={Binding Path=TheItems}
    SelectedItem={Binding Path=CurrentSelection} ... >

Everything works fine if the binding on the SelectedItem is not there. As soon as I add it, I get an exception in DataGrid.MakeFullRowSelection on line EndUpdateSelectionItems. Below is the call stack.
I tried using SelectedValue: same result. I tried using SelectedIndex: no problem. I tried forcing the selection mode to single: doesn't help.
Is there something I am missing??

gabriel

    System.Text.StringBuilder.FormatError()
    System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    System.String.Format(IFormatProvider provider, String format, Object[] args)
    System.Diagnostics.TraceListener.TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, String format, Object[] args)
    System.Diagnostics.TraceSource.TraceEvent(TraceEventType eventType, Int32 id, String format, Object[] args)
    MS.Internal.AvTrace.Trace(TraceEventType type, Int32 eventId, String message, String[] labels, Object[] parameters)
    MS.Internal.TraceData.Trace(TraceEventType type, AvTraceDetails traceDetails, Object p1, Object p2)
    System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)
    System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
    System.Windows.Data.BindingExpressionBase.UpdateValue()
    System.Windows.Data.BindingExpression.Update(Boolean synchronous)
    System.Windows.Data.BindingExpressionBase.Dirty()
    System.Windows.Data.BindingExpression.SetValue(DependencyObject d, DependencyProperty dp, Object value)
    System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, OperationType operationType, Boolean isInternal)
    System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
    System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties()
    System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
    System.Windows.Controls.SelectedItemCollection.EndUpdateSelectedItems()
    System.Windows.Controls.Primitives.MultiSelector.EndUpdateSelectedItems()
    Microsoft.Windows.Controls.DataGrid.MakeFullRowSelection(Object dataItem, Boolean allowsExtendSelect, Boolean allowsMinimalSelect) dans D:\dd\WPF_TSV_2\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:ligne 4663
    Microsoft.Windows.Controls.DataGrid.HandleSelectionForCellInput(DataGridCell cell, Boolean startDragging, Boolean allowsExtendSelect, Boolean allowsMinimalSelect) dans D:\dd\WPF_TSV_2\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:ligne 4382
    Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e) dans D:\dd\WPF_TSV_2\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:ligne 764
    Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e) dans D:\dd\WPF_TSV_2\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:ligne 726
    System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
    System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
    System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
    System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
    System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
    System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
    System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
    System.Windows.Input.InputManager.ProcessStagingArea()
    System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
    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, Int32 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, Boolean isSingleParameter)
    System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
    System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
    System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
    System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
    MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
    MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
    System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
    System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
    System.Windows.Threading.Dispatcher.Run()
    System.Windows.Application.RunDispatcher(Object ignore)
    System.Windows.Application.RunInternal(Window window)
    System.Windows.Application.Run(Window window)
    System.Windows.Application.Run()
    DataGridSelectedItem.App.Main() dans D:\Dev\Experiments\DataGridSelectedItem\obj\Debug\App.g.cs:ligne 0
    System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
    System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    System.Threading.ThreadHelper.ThreadStart()
Apr 17, 2009 at 12:03 PM
I forgot to mention something rather important:
To make it crash, you simply have to select the new item row placeholder.

gabriel
Apr 20, 2009 at 2:39 AM
Can someone confirm that this is indeed a bug?
Thanks!

gabriel
Coordinator
Apr 21, 2009 at 12:32 PM
This is a bug and has been logged in our internal database.
Jul 9, 2009 at 2:27 PM

Vinsibal - is this bug in the CodePlex "Issue Tracker" - I can't find it.

Coordinator
Jul 10, 2009 at 8:53 PM

No, the bug is filed in the internal bug database that we use within Microsoft to track our bugs.  It is not accessible by the public.

Jul 13, 2009 at 7:52 PM

FYI: The problem still exists with the DataGrid included in .Net 4 Beta 1.

I searched Connect and couldn't find an existing bug; reported this:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=474144