I have a dictionary<string, string[]>. I've bound the keys to a combobox but I've hit a blocker trying to bind the string array to a listview that will dynamically update depending on what key is selected. I'm able to bind the first array to the listview but I'm not sure what I'm missing to get the view to toggle between keys -
Model -
public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
ViewModel -
public class ViewModel
{
private Dictionary<Person, string[]> _person;
public Dictionary<Person, string[]> Persons
{
get { return _person; }
set { _person = value; }
}
private int _selectedPersonIndex;
public int SelectedPersonIndex
{
get { return _selectedPersonIndex; }
set
{
_selectedPersonIndex = value;
}
}
public ViewModel()
{
Persons = new Dictionary<Person, string[]>();
Persons.Add(new Person() { Name = "Mark" }, new string[] { "shirt", "pants", "shoes" });
Persons.Add(new Person() { Name = "Sally" }, new string[] { "blouse", "skirt", "boots", "hat" });
}
}
XAML -
<Window x:Class="MVVM_Combobox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodel="clr-namespace:MVVM_Combobox.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewmodel:ViewModel x:Key="vm"></viewmodel:ViewModel>
</Window.Resources>
<StackPanel Orientation="Vertical" DataContext="{Binding Source={StaticResource vm}}">
<ComboBox Width="200"
VerticalAlignment="Center"
HorizontalAlignment="Center"
x:Name="cmbTest"
ItemsSource="{Binding Path=Persons.Keys}"
SelectedValue="{Binding Path=SelectedPersonIndex}"
DisplayMemberPath="Name"/>
<ListView ItemsSource="{Binding Path=Persons.Values/}"
x:Name="myListView">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected,
RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Window>
by adding
<ListView ItemsSource="{Binding Path=Persons.Values/}"
It gets me the array value of the first key even before the key is selected from the combobox