0

I have a WPF DataGrid which I'm trying to auto-scroll using this code:

private void mydatagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    mydatagrid.ScrollIntoView(mydatagrid.CurrentItem);
}

The problem I'm having is that this event handler is not called every time the SelectedIndex of the DataGrid changes. Yes, it does get called when I click on the grid or add or delete an item, but it does NOT get called upon some other actions, such as moving the currently selected item up or down in the underlying collection, which is set via:

mydatagrid.ItemsSource = Seq;

(where Seq is an object of a type derived from ObservableCollection<>).

In my troubleshooting attempts I bound the text of a TextBlock to the SelectedIndex property of mydatagrid. The index number updates perfectly, regardless of what action I perform on the datagrid and/or underlying data source.

So my question is: how can I create an event handler in C# code that gets called upon ANY and ALL changes to the value of SelectedIndex, just like my little textblock binding example does?

I've tried finding a propertyChanged event for the DataGrid, but to no avail. I've tried tapping into all of the events of the DataGrid that seemed remotely related, but to no avail. I've also tried using the CollectionChanged event of the underlying collection, but this doesn't seem to be nicely synchronized with the datagrid (at least at the moment that the event occurs).

Thanks

BCA
  • 7,776
  • 3
  • 38
  • 53
  • I tried all your scenarios and cannot seem to reproduce it. I am able to fire the selectionChanged event when I set the selectedIndex to something, I also tried moving the selectedItem through up/down arrows or clicking it. Could you give more code please – 123 456 789 0 Aug 28 '12 at 02:09
  • Hi Leo, the main action that is NOT resulting in the firing of the SelectionChanged event is the Seq.Move(srcIdx, destIdx); method call. – BCA Aug 29 '12 at 15:46

0 Answers0