开发者

Equivalent of WinForms ComboBox.DroppedDown in WPF

I have some simple example WinForms code which I am trying to translate into WPF. The idea is to change the items within a ComboBox if a certain item is picked and drop down the ComboBox again if this happened. The WinForms code is:

 if (list.Text.Equals("C>>"))
        {
            comboBox1.Items.Clear();

            comboBox1.Items.Add("<<");
            comboBox1.Items.Add("C1");
            comboBox1.Items.Add("C2");
            comboBox1.Items.Add("C3");
            comboBox1.Items.Add("C4");
            comboBox1.Items.Add("C5");
            comboBox1.Items.Add("C6");
            comboBox1.DroppedDown = true;
        }

Although I though that this would be quite a simple change, using

    private void hotListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (hotListBox.SelectedItem != null)
        {
            if (hotListBox.SelectedItem.Equals("b >>"))
            {
                hotListBox.ItemsSource = secondList;
                hotListBox.IsDropDownOpen = true;
            }
            else if (hotListBox.SelectedItem.Equals("<<"))
            {
                hotListBox.ItemsSource = initialList;
                hotListBox.IsDropDown开发者_StackOverflow社区Open = true;

            }
            else if (hotListBox.SelectedItem.Equals("d >>"))
            {
                hotListBox.ItemsSource = thirdList;
                hotListBox.IsDropDownOpen = true;

            }
        }
    }

in WPF doesn't seem to work in the same way. I was wondering if anyone knew how to do this?

As pointed out in comments, I should say that the items in the ComboBox update as expected, but it doesn't drop down again in the WPF code.

Cheers,

EDIT: updated code


Change this:

hotListBox.IsDropDownOpen = true;

to this:

Application.Current.Dispatcher.BeginInvoke(new Action(delegate
{
     hotListBox.IsDropDownOpen = true;
}));


It's probably easier to just change the itemsource of the combobox when the list selection changes.

So create a couple of lists

   List<string> list1 = new List<string>() { "<<", "C1", "C2", "C3", "C4", "C5", "C6" };
   List<string> list2 = new List<string>() { "<<", "f", "g", "h", "i" };

and then just change the combobox item source on the list selection changed (using whatever logic you need)

private void _lbTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (_lbTest.SelectedIndex == 0)
        _comboTest.ItemsSource = list1;
    else
        _comboTest.ItemsSource = list2;

    _comboTest.IsDropDownOpen = true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜