Page view counter
Programmatically show/activate autocompletebox dropdown Subscribe to this thread
Last post 01-20-2009 5:35 AM by JWilcox. 4 replies.
Sort Posts:
01-05-2009 10:08 AM
Programmatically show/activate autocompletebox dropdown

Is it possible to programmatically show/activate the dropdown used by a autocompletebox control?

I aim at having the autocompletebox show all items in a list without the user typing a filter.

For example doing:

...
acb.IsTextCompletionEnabled =
true;
acb.SearchMode = AutoCompleteSearchMode.Contains;
acb.ItemsSource = myStringArray;
acb.Text =
"z";

In the above case I would like to show the dropdown with all strings containing "z" from myStringArray.

Best Regards,
Thomas Hagen

KaptajnVom

Loading...
Joined on 06-30-2008
Posts 6
01-05-2009 11:42 AM
Marked as Answer
Re: Programmatically show/activate autocompletebox dropdown

Thomas,

Technically the auto complete control does not expose a method for doing this, but if you look through the unit tests that came with the toolkit (AutoCompleteBoxTest.cs), you'll see that those do exactly what you are looking to do.

The process is:

1. Step into the visual tree to find the TextBox that is part of the auto complete control

2. Programatically set the Text dependency property value on the TextBox (not auto complete box) to your value ("z")

That should work just fine. Let me know if you need help locating the test or getting this working.

-Jeff

Jeff Wilcox [MSFT]
Software Development Engineer
Silverlight
http://www.jeff.wilcox.name/blog/

This posting is provided "AS IS" with no warranties, and confers no rights.

JWilcox

Loading...
Joined on 07-14-2005
Seattle
Posts 175
01-06-2009 11:01 AM
Re: Re: Programmatically show/activate autocompletebox dropdown

Thanks Jeff.

For future readers/reference:

1. The VisualTree must be traversed after the control has Loaded - otherwise you risk that Textbox is not created (== null)

2. The flesh of the code is:
void AutoCompleteBox_Loaded(object sender, RoutedEventArgs e)
{
  DependencyObject o = VisualTreeHelper.GetChild((AutoCompleteBox)sender, 0);
  o =
VisualTreeHelper.GetChild(o,0);
  ((
TextBox)(o)).Text = "z";
}

 

Best Regards,
Thomas Hagen

KaptajnVom

Loading...
Joined on 06-30-2008
Posts 6
01-20-2009 2:33 AM
Re: Re: Re: Programmatically show/activate autocompletebox dropdown

Additionally, is there a way to programmatically select and display an item from the autocompletebox ItemsSource?

I want to use the same control, an AutoCompleteBox, both for searching and displaying data.

mikejos

Loading...
Joined on 01-12-2009
Posts 7
01-20-2009 5:35 AM
Re: Re: Re: Programmatically show/activate autocompletebox dropdown

@mikeros,

If you're simply looking to set the SelectedItem, or use a two-way binding, the next release of the toolkit will support this. In the meantime, you can get early access to the changes here: http://www.jeff.wilcox.name/2008/12/selecteditem-dcr/ 

-Jeff

Jeff Wilcox [MSFT]
Software Development Engineer
Silverlight
http://www.jeff.wilcox.name/blog/

This posting is provided "AS IS" with no warranties, and confers no rights.

JWilcox

Loading...
Joined on 07-14-2005
Seattle
Posts 175
Microsoft Communities