Social Icons

Pages

WPF Change ListView based on Item Chosen in ComboBox


Here I'm going to describe simple way to display items in a ListView based on the Item selected on a ComboBox. Ultimate result would be like below images;



Step one: Create classes for items of both Combobox and ListView.
public class Item
{
public int itemID { get; set; }
public string Name { get; set; }
public Item(int ID, string name)
{
this.itemID = ID;
this.Name = name;
}
}
public class SubItem
{
public SubItem(int subItemID, Item apple, string SubName)
{
this.subItemID = subItemID;
this.Item = apple;
this.SubName = SubName;
}
public int subItemID { get; set; }
public Item Item { get; set; }
public string SubName { get; set; }
}
public class DataContext
{
public List<Item> itemList { get; set; }
public List<SubItem> subItemList { get; set; }
public DataContext()
{
Item apple = new Item(1, "Apple");
Item orange = new Item(2, "Orange");
Item banana = new Item(3, "Banana");
itemList = new List<Item>();
itemList.Add(apple);
itemList.Add(orange);
itemList.Add(banana);
SubItem AppleA = new SubItem(1, apple, "Vitamin A");
SubItem AppleB = new SubItem(2, apple, "Vitamin B");
SubItem AppleD = new SubItem(3, apple, "Vitamin D");
SubItem OrangeC = new SubItem(4, orange, "Vitamin C");
SubItem OrangeD = new SubItem(5, orange, "Vitamin D");
SubItem BananaK = new SubItem(6, banana, "Vitamin K");
SubItem BananaA = new SubItem(7, banana, "Vitamin A");
SubItem BananaE = new SubItem(8, banana, "Vitamin E");
subItemList = new List<SubItem>();
subItemList.Add(AppleA);
subItemList.Add(AppleB);
subItemList.Add(AppleD);
subItemList.Add(OrangeC);
subItemList.Add(OrangeD);
subItemList.Add(BananaK);
subItemList.Add(BananaA);
subItemList.Add(BananaE);
}
}
Step two: Design the application interface as below;
<StackPanel>
<ComboBox SelectedItem="{Binding Item}" ItemsSource="{Binding itemList}"
Margin="105,56,52,0" Height="27" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ListView Name="VitaminsListView" SelectedItem="{Binding SubItem}"
ItemsSource="{Binding subItemList}" HorizontalAlignment="Left"
Height="201" Margin="105,50,0,0" VerticalAlignment="Top" Width="283">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="ID" DisplayMemberBinding="{Binding subItemID}"/>
<GridViewColumn Header="Vitamins" DisplayMemberBinding="{Binding SubName}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
Step three: Code behind the main window,
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new DataContext();
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Item selected = (Item)(sender as ComboBox).SelectedItem;
DataContext data = new DataContext();
List<SubItem> subItem = data.subItemList.FindAll(c => c.Item.Name == selected.Name);
VitaminsListView.ItemsSource = null;
VitaminsListView.Items.Clear();
VitaminsListView.ItemsSource = subItem;
}
}

Please comment below if you have any questions.
Enjoy !



1 comment:

  1. datacontext is a type but it is used like a variable.
    listview does not contain a definition for itemssource and no extension method itemsource accepting first argument of type listview could be found(are you missing a using directive or an accembly reference ) error

    ReplyDelete