views:

642

answers:

1

Basically what I want to do is allow a user to type in a string value and have the list box scroll to the item that matches the text they have typed (or the first LIKE match).

If I use the .ScrollIntoView() method, the problem is that it puts the item at teh bottom of the visible area if the item is further down in the list than the current scroll position, and it is at the top if it is higher in the list than the current scroll position. I want to make it consistent by making it the top item in the list (unless of course it can't be due to being one of the last "page" of items).

I have tried to fake it by selecting the item which is x further down in the list where x is the number of items visible. This works when going down but breaks when going back up. and I've been unable to determine which index is currently the one at the top visible spot in my list.

WinForms list boxes have the .TopIndex property which des exactly what I'm looking for, but I've been unable to find the WPF equivalent. Anyone out there have an idea how to accomplish this?

Thanks in advance!

+2  A: 

Use ScrollIntoView twice, first to show the very last item in the list, then to show your selected item. This way it will always be working from the bottom up. You'll need to call UpdateLayout after each call to make sure the positions are correct.

Chris Blackwell
I had to call UpdateLayout before the last call, otherwise I would get this tug-o-war in the list between the target item and last item (constant scrolling up and down between them.That was the right track tho, thanks!
palehorse
Glad it worked, I edited my response to reflect what you found while implementing.
Chris Blackwell