0

I have a big list (~10000 items) inside a ComboBox, that uses the VirtualizingStackPanel class. The scrolling performance is good, anyway I like to know how to identify the top item shown in the GUI - which is, not necessarily the one with index zero, but the one that is on top of the current scrolled item list. ? Thank you.

deafjeff
  • 754
  • 7
  • 25
  • Are you binding the list to a collection of items? Or are you adding them manually? Also, are you saying you want the first item in the current view? So, if item 52 were the first visible item, you would be able to identify 52? –  Jul 09 '13 at 14:17
  • the combobox is bound to a collection via ItemsSource, and Yes i want 52, if it's on top. – deafjeff Jul 09 '13 at 14:27

1 Answers1

0

I use the FrameworkElement.IsVisible property. Using the method at the link below, you can also check to see if an element is partially visible(which would be useful in your case):

In WPF, how can I determine whether a control is visible to the user?

You could (by using a background thread or by doing a computation when the user scrolls in the combobox), check the items in the ComboBox to see if their FrameworkElement.IsVisible property were set to true. If so, you update that elements IsVisible property in its ViewModel. Now you have a collection in your ViewModel of items that are marked visible or not(and that are constantly updated concerning the visibility of the CoboBox item that it represents). Now you can find which is the first using .First(x=>x.IsVisible==true) on the collection of items.

Community
  • 1
  • 1
  • I have no idea how to retreive the Frameworkelement, because I actually operate on the viewmodel only, and have set the Combobox ItemsSource. – deafjeff Jul 10 '13 at 10:56
  • Your answer is a good approach, but are there really instances of ComboBoxItems, if you have set the ItsmSource ? For example, when you want to operate on the Items memebr, you get an exception because you have ItemsSource set. – deafjeff Jul 10 '13 at 11:00
  • I just read that by specifying ItemsSource, the Items are auto-generated by the wpf system. That explains they're not accessible by the Items member. That makes it even a more challenging task to retrieve the belonging framework element. Any help appreciated. – deafjeff Jul 10 '13 at 12:33
  • 1
    Right, they are auto generated, but you can still access the individual elements from the ComboBox. For example, if you put an event handler for a ComboBox scroll in the xaml.cs file, a System.Object sender will be returned. You should be able to cast this to a FrameworkElement and access the objects(the ComboBox items) inside the sender. –  Jul 10 '13 at 17:05
  • Thanks, it works fine with OnScrollChanged, where EventArgs.VerticalOffset is equal to the ItemsSource index. – deafjeff Jul 11 '13 at 13:00