Welcome to the Xceed Community | Help
Community Search  
More Search Options

Scrollbar does not go back to top after sort has changed

Sort Posts: Previous Next
  •  04-26-2011, 2:52 PM Post no. 30246

    Scrollbar does not go back to top after sort has changed

    I have virtualized grids where everything works fine but I have one grid that does not use the DataVirtualization and here is the issue.

     

    I have lets say 50 records in the grid, and initially scrollbar is on the top as expected.  If I sort by some column in the grid everything works as expected.  Data is sorted by the column and the Scrollbar is at the top showing first item based on scrolling event (asc,dsc).

     If I do following it does not work. If I have default loaded data in the grid with 50 records, and then scroll down to the last record.  If I now try to sort ASCENDING by some column, the scrollbar goes to the top of the grid as expected, but when I scroll by the same column again DESCENDING, then the scrollbar goes to the bottom of the grid, and It should be showing actually top of the grid all the time, so I can see the items I sorted.

     

    Edit:

    <xcdg:DataGridCollectionViewSource x:Key="DataSource"
                                                   Source="{Binding Data}"
                                                   ItemType="do:Data">
                    <xcdg:DataGridCollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="Name"
                                             Direction="Descending" />
                    </xcdg:DataGridCollectionViewSource.SortDescriptions>
                </xcdg:DataGridCollectionViewSource>

     

    I have the  DataGridCollectionViewSource and now if I do, as told on the xceed docs site to subscribe to event

     

    protected override void OnInitialized( EventArgs e )
    {
    base.OnInitialized( e );
    DataGridCollectionView view = this.OrdersGrid.ItemsSource as DataGridCollectionView;
    ( ( INotifyCollectionChanged )view.SortDescriptions ).CollectionChanged +=
    new NotifyCollectionChangedEventHandler( this.SortCollectionChanged );
    }
    private void SortCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
    {
    Debug.WriteLine( "Sort changed" );
    }

     my  view is allways null? 

     Does anybody know how to fix this, without using some crazy hack around?

     

    Thanks

  •  04-27-2011, 2:18 PM Post no. 30254 in reply to 30246

    Re: Scrollbar does not go back to top after sort has changed

    Hi Nikola,

    In the constructor, you must subscribe to the ItemsSourceChangeCompleted event so that after the sorting is done, it will bring you to the first item on the DataGrid. For example:

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    this.NameOfDataGrid.ItemsSourceChangeCompleted += new EventHandler(NameOfDataGrid_ItemsSourceChangeCompleted); 

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     

     

    Next, in the event handler, you must code the following. It will all you to subscribe to the NotifyCollectionChangedEventHandler on the SortDescriptions to that it can be triggered after the sorting:

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    void Customers_ItemsSourceChangeCompleted(object sender, EventArgs e)

    {

           DataGridCollectionView sourceView = this.NameOfDataGrid.ItemsSource as DataGridCollectionView;

           if (sourceView != null)

           {

               // observe the Sorting Collection Changed event

               System.Collections.Specialized.INotifyCollectionChanged sortingCollection = (System.Collections.Specialized.INotifyCollectionChanged)sourceView.SortDescriptions;

               sortingCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(sortingCollection_CollectionChanged);

           }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     

    Next, you must code the following to call the method to bring the first item in the DataGrid into view using a Dispatcher call. 

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    void sortingCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)

    {

           Action action =()   =>

           {

               GetFirst() ;

           };

           Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, action);

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     

     

    Lastly, you must code the method that is being called to bring the first item on the DataGrid with the sorted items intoview

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private void GetFirst()

    {

           this.NameOfDataGrid.BringItemIntoView(this.Customers.Items[0]);

           this.NameOfDataGrid.SelectedItem = this.Customers.Items[0];

           this.NameOfDataGrid.CurrentItem = this.Customers.Items[0];

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Marc

    Developer in Technical Support
    Xceed - Multi-talented components - http://xceed.com
  •  04-27-2011, 5:53 PM Post no. 30255 in reply to 30254

    Re: Scrollbar does not go back to top after sort has changed

    Thanks,

     this is exatcly what I needed.  I guess I had to subscribe to the event on ItemsSourceChangeCompleted.

     Nikola

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.