ShouldExtendSelection false first time DataGrid is loaded

Nov 5, 2009 at 12:47 AM

I am using a MVVM pattern with the DataGrid.  The first time the data grid is loaded the user cannot use key modifiers to modify the selection. 

In my ViewModel I set a SelectedItem property as the first or default item in my collection.  This item is bound to the SelectedItem dependency property on the DataGrid control and is visibly selected.  If, on the very first time the DataGrid is loaded, the user wants to extend the selection to include the first row, the default selected row, through the fifth row they hold the shift key down and click on the fifth row.  This is default behavior for list selection.  The result is not consistent with default list behavior.  Instead of selecting all five rows the action only selects only the fifth row

By stepping through the code it looks like the _selectionAncor in the ShouldExtendSelection property's getter is null in this case, and only in this case.  Is this by design? Is this an issue that is resolved in the .Net 4.0 version of the DataGrid?

       /// <summary>
        ///     SHIFT is down or performing a drag selection.
        ///     Multiple items can be selected.
        ///     There is a selection anchor.
        /// </summary>
        private bool ShouldExtendSelection
                return CanSelectMultipleItems && (_selectionAnchor != null) &&
                    (_isDraggingSelection || ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift));

We are working around the problem by not setting the SelectedItem property on our view model to the first or default item in our collection.  This forces the user to select an item and avoids the undesired behavior we are currently seeing.