views:

330

answers:

2

Lets say I have an AutoCompleteBox with 1000 items.

  1. A user first types two characters returning a broad result set (for example, 100 items).
  2. They then scroll to the bottom of the list and...
  3. They then select the last item which closes the dropdown.
  4. The user then returns to the AutoCompleteBox and enters a more refined search returning, say, 25 items.
  5. Because the previously selected item was at the bottom of the dropdown, the AutoCompleteBox shows the last element(s) of the new search result, instead of displaying the first items at the top of the list.

How do I force the AutoCompleteBox to display the results starting with the first item in the list?

+1  A: 

Here is a blind guess based on the default control templates. In DropdownOpening event:-

 var sv = ((FrameworkElement)sender).FindName("ScrollViewer") As ScrollViewer;
 if (sv != null)
   sv.VerticalOffset = 0;
AnthonyWJones
Ooooh... so close. I thought it was going to work for a minute, but got this error: "System.Windows.Controls.ScrollViewer.VerticalOffset' cannot be assigned to -- it is read only" But Thanks! You just made a positive adjustment to my understanding of the object.
Traples
PS... the ScrollViewer is not found in the sender (AutoCompleteBox) anyway.
Traples
+1  A: 

I know this is not much of an answer, but I thought I would share what I discovered, even though it does not solve the problem. I tried all the following in the DropDownOpeningevent.

  1. Set the SelectedItem to null. - Failed.
  2. Follow what Anthony suggested. - Failed. First off, sender is AutoCompleteBox and does not contain ScrollViewer. Second, VerticalOffset is read-only.
  3. I checked all the variables in AutoCompleteBox and found that it has a non-public variable called DropDownPopup that is an instance of System.Windows.Control.PopupHelper. I could not find it in that library though. It does however, contain a Popup property with a variable VerticalOffset. However, I tried to inherit AutoCompleteBox and was unable to access that variable, so I suspect it must be private.
Johannes