Skip to main content

Microsoft Silverlight

Answered Question ComboBox SelectedItem not visible when dropdown activatedRSS Feed

(0)

DukeAmes
DukeAmes

Member

Member

2 points

6 Posts

ComboBox SelectedItem not visible when dropdown activated

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
lingbing

Contributor

Contributor

2249 points

406 Posts

Answered Question

Re: ComboBox SelectedItem not visible when dropdown activated

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!

Ling Bing
Bei Jing University of Aeronautics and Astronautics
Bei Jing, China

Reeta Lodhi
Reeta Lodhi

Member

Member

83 points

16 Posts

Answered Question

Re: ComboBox SelectedItem not visible when dropdown activated

Try to set the SelectedIndex on page loaded event.

void Window1_Loaded(object sender, RoutedEventArgs e)
        {
           myComboBox.SelectedIndex=0;

        }

Reeta
http://reeta-singh.blogspot.com/
http://www.cerebrata.com/

DukeAmes
DukeAmes

Member

Member

2 points

6 Posts

Re: Re: ComboBox SelectedItem not visible when dropdown activated

Thanks very much for your answer Ling; worked very well Smile

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,

-Duke

DukeAmes
DukeAmes

Member

Member

2 points

6 Posts

Re: Re: ComboBox SelectedItem not visible when dropdown activated

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!

-Duke 

 

 

lingbing
lingbing

Contributor

Contributor

2249 points

406 Posts

Re: Re: ComboBox SelectedItem not visible when dropdown activated

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!

Ling Bing
Bei Jing University of Aeronautics and Astronautics
Bei Jing, China
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities