Nodon
Nodon

Reputation: 1027

Binding Dictionary<String, Int32> by keys to ListBox and selected item values to ComboBox

I have a Dictionary<String, Int32[]> which is named someDict. Keys are binding to ListBox.ItemsSource. Values of ListBox.SelectedItem must to be binding to ComboBox.ItemsSource. How to do it?

<ListBox x:Name="listBox" Grid.Column="0" ItemsSource="{Binding Path=someDict.Keys}"/>
<ComboBox x:Name="comboBox" Grid.Column="1" VerticalAlignment="Top" 
          ItemsSource="{Binding ElementName=comboBox, Path=SelectedItem}"/>

Upvotes: 1

Views: 2659

Answers (1)

enapi
enapi

Reputation: 728

You have to make some changes to your XAML.

  1. ItemsSource of your ListBox should be the whole Dictionary. To diplay Key values use an ItemTemplate

  2. Modify the ItemsSource of a ComboBox

    <ListBox x:Name="listBox" Grid.Column="0" ItemsSource="{Binding Path=someDict}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Key}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <ComboBox x:Name="comboBox" Grid.Column="1" VerticalAlignment="Top" ItemsSource="{Binding ElementName=listBox, Path=SelectedItem.Value}"/>
    

Upvotes: 4

Related Questions