Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use ScrollIntoView() with a PagedCollectionView in a Silverlight DataGrid?

Is it possible to scroll to a particular row (by object identity) in a Silverlight DataGrid that has an ItemsSource which is a PagedCollectionView.

I am loading a list of orders that are grouped by day/status etc. I need to be able to scroll to a particular order.

 var pcv = new PagedCollectionView(e.Result.Orders);
 gridOrders.ItemsSource = pcv;

Unfortunately, ScrollIntoView(order) doesn't work because of the PagedCollectionView.

An article on DataGrid from MSDN shows that it is possible to scroll to a group in a PagedCollectionView, but that's not really much use.

  foreach (CollectionViewGroup group in pcv.Groups)
  {
       dataGrid1.ScrollIntoView(group, null);
       dataGrid1.CollapseRowGroup(group, true);
  }

Is there a way to do this ?

like image 391
Simon_Weaver Avatar asked Jan 30 '10 23:01

Simon_Weaver


1 Answers

Yes, it is possible to scroll items into view when the item source is a PagedCollectionView. I use both the group scrolling method you describe and I scroll the currently selected item into view. To do this, I have a helper method that uses the dispatcher to invoke the operation as follows:

private void ScrollCurrentSelectionIntoView()
{
    this.dataGrid.Dispatcher.BeginInvoke(() =>
    {
        this.dataGrid.ScrollIntoView(
            this.dataGrid.SelectedItem,
            this.dataGrid.CurrentColumn);
    });
}

I used BeginInvoke because otherwise, the call to ScrollIntoView would fail when called directly from an event handler (presumably because the DataGrid hadn't properly updated its state for the event being handled). This approach ensures that the current event handling completes properly before invoking the scroll.

like image 109
Jeff Yates Avatar answered Sep 30 '22 04:09

Jeff Yates