0

I have a grid which ItemsSource is bound to a list of string. When I remove an element from the ItemsSource, the ScrollBar moves to the removed element, which is problematic for me.

For example, my grid contains 100 rows. The user is focusing the 100th row. The user gives the order to remove the first row: the ScrollBar moves to the second row.

Is there a way to avoid this behavior?

Thanks

Morgan M.
  • 846
  • 2
  • 9
  • 27

1 Answers1

0

Show us your code please. We would like to see how have you set the preferences to your grid. Futhermore tell us are you resetting the value of ItemsSource when you change something in your list? Are you using ObservableCollection?

Anyways take a look at this propertly. Maybe this is what you looking for:

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.issynchronizedwithcurrentitem%28v=vs.110%29.aspx

<ListBox Name="employeeListBox1"
         ItemsSource="{Binding Source={StaticResource Employees}}"
         ItemTemplate="{StaticResource EmployeeItemTemplate}"
         IsSynchronizedWithCurrentItem="True"/>

IsSynchronizedWithCurrentItem should prevent scrollbars to be moved without your permissions. :)

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55
  • When I add or remove an element in the itemsSource, I don't reset it, I use the Add() / AddRange() and Remove() / RemoveRange() methods. My ItemsSource is not an ObservableCollection, but this doesn't prevent the interface to be updated. Unfortunately, the IsSynchronizedWithCurrentItem property doesn't change the behavior. – Morgan M. Oct 28 '13 at 11:35
  • You should make your list an ObservableCollection. – dev hedgehog Oct 28 '13 at 11:44
  • Yes I should... I thought it was but I just realized after you asked it that it is not. Could this be the source of my issue? – Morgan M. Oct 28 '13 at 11:46
  • Not sure since you havent posted us code of exactly what you up to. I dont know what you up to. We must see xaml and code behind and viewmodel to understand your issue. I dont know why you not using ObservableCollection. Is there a reason why you decided to use List? I can just keep guessing cases which usually appear. :) Try it with ObservableCollection. – dev hedgehog Oct 28 '13 at 11:49
  • Actually, the reason I'm not using an ObservableCollection is because this List of string is built in a WCF service, and retrieved from the WPF interface. What would be the best solution: convert it from List to ObservableCollection in my interface, or define it as an ObservableCollection in the service (not really possible I guess)? – Morgan M. Oct 28 '13 at 11:53
  • You can pass list to ObservableVollection constructor. Take a look at this link: http://msdn.microsoft.com/en-us/library/ms668604.aspx – dev hedgehog Oct 28 '13 at 12:02
  • Yes, thank you :) Anyway, converting my ItemsSource from a List to an ObservableCollection didn't solve my issue. – Morgan M. Oct 28 '13 at 14:18
  • How about you give us more code? I will stop guessing what I might be. :) :) :) – dev hedgehog Oct 28 '13 at 14:20