开发者

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

开发者 https://www.devze.com 2022-12-19 04:02 出处:网络
Is it possible to scroll to a particular row (by object identity) in a Silverlight DataGrid that has an ItemsSource which is a PagedCollectionView.

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, tr开发者_如何学Goue);
  }

Is there a way to do this ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号