views:

28

answers:

2

Hi all,

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.IsDropDownOpen = 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

A: 

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;
}
mdm20
Although this makes the code a lot neater (which is nice), it doesn't seem to solve the issue of the ComboBox not dropping down when .IsDropDownOpen is set :(
C A
You need to post more code then, because it opens up fine for me using that code.
mdm20
Updated code in my original post.
C A
+2  A: 

Change this:

hotListBox.IsDropDownOpen = true;

to this:

Application.Current.Dispatcher.BeginInvoke(new Action(delegate
{
     hotListBox.IsDropDownOpen = true;
}));
La Cabra
Worked, thanks - I wish I knew exactly why (pretty new to C# and WPF in general), I feel it's to the books I go!
C A