Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit ComboBox SelectedItem not visible when dropdown activated
5 replies. Latest Post by lingbing on July 2, 2009.
(0)
DukeAmes
Member
2 points
6 Posts
07-01-2009 9:37 PM |
If I set the SelectedItem or Index when the I initialize the page, the item is not visible in the dropdown list. The user has to scroll to see it.
What is the fix for this?
Thanks,
-Duke
lingbing
Contributor
2249 points
406 Posts
07-01-2009 11:10 PM |
Hi, it is a pity that ComBoBox doesn't has ScrollToView method as ListBox, when you set SelectedItem or SelectedIndex, if the Dropdown is not open, it will just change the ToggleButton's content. However, if the Dropdown is open, the selected ComboxItem will get focus, then it is scrolled to view itself.
So there is a workaround, you can use Dispatcher.BeginInvoke, it will like:In xaml:<ComBoBox x:Name="Box"/>in xaml.cs:private void InitComBoBox(){ Box.ItemSource=xxxxx;// Anything you want Dispatcher.BeginInvoke(delegate { Box.IsDropDownOpen=true;//open the dropdown Dispatcher.BeginInvoke(delegate { Box.SelectedIndex=xxxx;// Any index you want //Or Box.SelectedItem=xxxx; Any object you want Box.IsDropDownOpen=false;// Close the dropdown }); });}
Because you open the dropdown the index you selected will get focus and scroll to view, and then you close it, user will not see the dropdown, everything is ok, this trick will not be found by end user.
Regards!
Reeta Lodhi
83 points
16 Posts
07-02-2009 5:25 AM |
Try to set the SelectedIndex on page loaded event.
void Window1_Loaded(object sender, RoutedEventArgs e) { myComboBox.SelectedIndex=0;
}
07-02-2009 11:01 AM |
Thanks very much for your answer Ling; worked very well
That being said I'm new to Silverlight, and was a bit confused with your code, looking for an Init event. I changed your code to make it more clear:
<ComboBox x:Name="MyComboBox"/>in xaml.cs:private void PopulateMyComboBox(){ MyComboBox.ItemSource=xxxxx;// Anything you want Dispatcher.BeginInvoke(delegate { MyComboBox.IsDropDownOpen=true;//open the dropdown Dispatcher.BeginInvoke(delegate { MyComboBox.SelectedIndex=xxxx;// Any index you want //Or MyComboBox.SelectedItem=xxxx; Any object you want MyComboBox.IsDropDownOpen=false;// Close the dropdown }); });}
Thanks again,
07-02-2009 11:06 AM |
Thanks for the reply Reeta,
I like the look of your solution, however I have no idea how to implement it.
Would you mind being more specific with your example? I have a Silverlight xaml page with a <Combobox x:Name="MyComboBox" /> and a xaml.cs code page.
Appreciate your help!
07-02-2009 11:53 AM |
Hi, duke, the silverlight page doesn't has an init event, however, it has an Loaded event, you can initialize/populate your ComboBox at the Loaded handler.
The Dispatcher.BeginInvoke(Action a) make that action be called when UI thread is idle, if you call MyComboBox.IsDropDownOpen=true at once after you set ItemsSource for that ComboBox, nothing will happen, and the inner Dispatcher.BeginInvoke uses as the same function.
Sorry for my poor English, wish my explaination is clear. Regards!