¿Cómo puedo filtrar los valores desplegables de otro menú desplegable en WPF C#?

I am unable to filter the dropdown in WPF form like in c# form..

I created a list for my scenario dropdown and used it as items source in LoadUserControl() método como se muestra a continuación.

  public class FilterListItem
  {
    public string Filter { get; set; }
  }

  public LoadUserControl()
  {
  InitializeComponent();
  List<FilterListItem> FilterList = new List<FilterListItem>();
  FilterList.Add(new FilterListItem() { Filter = "Make" });
  FilterList.Add(new FilterListItem() { Filter = "Model" });
  FilterList.Add(new FilterListItem() { Filter = "Year" });
  myscenario.ItemsSource= FilterList;
  myscenario.SelectedItem= "Model";
  }

Here's the code that i have for Script dropdown to filter as per the selection from scenarios dropdown.

private void myscenario_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {

 if (myscenarios.SelectedItem.ToString() == "Model")
    {
     Scriptlist.Add(script1);
     Scriptlist.Add(script2);
     break;
    }
 else if (myscenarios.SelectedItem.ToString() == "Make")
    {
     Scriptlist.Add(script3);
     Scriptlist.Add(script4);
     break;
    }

else
    {
     Scenariolist.Add(sc5);
     Scenariolist.Add(sc6);
    }
 }

When i execute the script, the UI is only going to default value which is model and whenever i select other value from dropdown, it still shows the selected item as "Model".

Also, When i debug for the value of the selected item from scenarios dropdown with the messagebox, i get this message as below.

**MessageBox.Show(myscenarios.SelectedItem.ToString()); 

//I get 'Filterlistitem' in message box instead of the selected item that i just selected.**

All i need is to filter the script dropdown as per the selection of the user in scenario dropdown like i mentioned above.

FYI, i didn't put the xaml UI just to make the question simpler.

Cualquier ayuda sería apreciada.

Muchas Gracias

amante del selenio

preguntado el 12 de junio de 12 a las 21:06

If you remove the myscenario_SelectionChanged event handler can you change the selected item? -

1 Respuestas

Parece que estás usando List<T> como el ItemsSource of your dropdown control which does not update the UI when the list changes. Use Colecciones observables to notify the UI when the collection changes.

Respondido el 12 de junio de 12 a las 23:06

Thank you for your reply. I'm very new to WPF. Can you explain how to do the above problem with ObservableCollections to notify the UI when the collection changes? - amante del selenio

En lugar de un List<T> utilizado ObservableCollection<T>. - Evanb

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.