Let the items in the ListBox depend on which item is selected in the ComboBox
I have a small WPF application with a ComboBox where the user can select from a list of items. I also have a ListBox where I want the available items in the ListBox to depend on what item is currently selected in the ComboBox.
Say the ComboBox has the following options: "Fruit" and "Vegetable". If I select Fruit, the ListBox will contain Apple, Banana, Pear, etc., and if I select Vegetables, it will contain Carrots, Potatoes, etc.
This is just a dummy example, but it covers what I need. In my application - both the data for the ComboBox and everything that needs to be placed in the ListBox will come from an external data source.
How can i do this? I have already bound my view model to the view and populated the ComboBox from the data source, but I need the contents of the ListBox to reflect the selected option in the ComboBox.
I appreciate any help!
thank
source to share
make 2 lists and link one of them to your list according to the selection. eg:
List <string> Fruits=new List<string>(){"apple","banana",..};
List <string> Vegetables=new List<string>(){"tomato","Potato",..};
and in the Combox selection change event:
private void OComboBox1Changed(object sender, SelectionChangedEventArgs e)
{
if (ComboBox1.Selected.Item=...)
{
listbox1.ItemsSource=Fruits;
}
else
{
listbox1.ItemsSource=Vegetables;
}
}
source to share
You can add DependencyProperty to your viewmodel containing SelectedItem
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
"SelectedItem",
typeof(YourType),
typeof(YourViewModel),
new FrameworkPropertyMetadata(
new PropertyChangedCallback(OnSelectedItemChanged)
)
);
public YourType SelectedItem
{
get { return (YourType)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
(Replace YourType with the vegetable / fruit type and YourViewModel with your viemodel type)
and bind this to your Combobox SelectedItem in XAML.
<ComboBox x:Name="comboBox" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
You also need to define a method to handle PropertyChangedCallback:
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Change the listbox item source here.
}
source to share
View:
<ComboBox ItemsSource = "{Binding Options}" SelectedItem = "{Binding SelectedOption}" Width = "200" / ">
<ListBox ItemsSource = "{Binding lst}" Grid.Row = "1">
ViewModel:
public class MainViewModel: INotifyPropertyChanged {
private string selectedOption;
public string SelectedOption
{
get
{
return this.selectedOption;
}
set
{
this.selectedOption = value;
this.UpdateOnOptionChange();
}
}
public List<string> Options
{
get;
set;
}
public ObservableCollection<string> lst
{
get;
set;
}
public MainViewModel()
{
this.Options = new List<string>() { "Fruits", "Vegetables" };
this.lst = new ObservableCollection<string>();
}
private void UpdateOnOptionChange()
{
this.lst.Clear();
if (this.selectedOption == "Fruits")
{
this.lst.Add("Apple");
this.lst.Add("Banana");
this.lst.Add("Pear");
}
else if (this.selectedOption == "Vegetables")
{
this.lst.Add("Carrot");
this.lst.Add("Potato");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyOnPropertyChange(string astrPropertyName)
{
if (null != this.PropertyChanged)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(astrPropertyName));
}
}
}
source to share