silverlightdatagridpagingsilverlight-toolkitpagedcollectionview

Silverlight: disable automatic first-row selection after DataGrid binding


I have a Silverlight DataGrid, a DataPager and a PagedCollectionView. After the PagedCollection view is bound to the DataGrid, the first row of the DataGrid is selected.

This behaviour does not happen, if i am using an ObservableCollection. But due to the DataPager, I need to use the PagedCollectionView.

I am using Silverlight 5 RC, and the SL 4 Toolkit.

Example of Usage:

View:

<sdk:DataGrid x:Name="gridSomeItems" ItemsSource="{Binding SomeItems, Mode=TwoWay}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" CanUserSortColumns="True">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Header="Column1" Width="*" Binding="{Binding Column1}" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>
<sdk:DataPager Source="{Binding Path=ItemsSource, ElementName=gridSomeItems}"/>

ViewModel:

public class SomeViewModel : ViewModelBase
{
    public SomeViewModel(IDataAccess dataAccess)
    {
        _dataAccess = dataAccess;
        _dataAccess.GetSomeItemsCompleted += GetSomeItemsCompleted;
        _dataAccess.GetSomeItems();           
    }

    public PagedCollectionView SomeItems { get; set; }
    public SomeItem SelectedItem { get; set; }

    private void GetSomeItemsCompleted(GetSomeItemsCompletedEventArgs e)
    {
        SomeItems = new PagedCollectionView(e.Result.SomeItems);         
        RaisePropertyChanged("SomeItems");
    }
}

Solution

  • The best workaround is to set a _isLoading flag. The following code would be the correct code for the ViewModel:

    public class SomeViewModel : ViewModelBase
    {
        private bool _isLoading;
    
        public SomeViewModel(IDataAccess dataAccess)
        {
            _dataAccess = dataAccess;
            _dataAccess.GetSomeItemsCompleted += GetSomeItemsCompleted;
            _isLoading = true;
            _dataAccess.GetSomeItems();           
        }
    
        public PagedCollectionView SomeItems { get; set; }
        public SomeItem SelectedItem { get; set; }
    
        private void GetSomeItemsCompleted(GetSomeItemsCompletedEventArgs e)
        {
            SomeItems = new PagedCollectionView(e.Result.SomeItems);         
            SomeItems.CurrentChanged += CurrentItemChanged;
            RaisePropertyChanged("SomeItems");
            SomeItems.MoveCurrentTo(null);
            _isLoading = false;     
        }
    
        private void CurrentItemChanged(object sender, System.EventArgs e)
        {
            if (!_isLoading)
            {
                SelectedItem = SomeItems.CurrentItem as SomeItem;
                // Do something more...
            }
        }   
    }
    

    Please have a look at the row SomeItems.MoveCurrentTo(null); - This command really sets the CurrentItem of the PagedCollectionView to null (not selected).

    It is still a workaround... Would be great if anybody came up with a better solution.