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