Search code examples
wpfxamldictionarycomboboxitemtemplate

How to make binding between comboBox item tag


I define Dictionary that contain int as key and string as value Dictionary<int, string >. I made binding between ComboBoxItem and this Dictionary:

<ComboBox ItemsSource="{Binding myDictionary}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Key}" Tag="{Binding Value}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Now, i want to make a Binding between the item selected tag. How can i do it ? I know how to do it in case i want to have the SelectedValue of the combo (get the text selected in the combo) ==> but i need the tag and not the text ... :(


Solution

  • You can bind to SelectedItem.Tag

    Example:

      <TextBlock Text="{Binding ElementName=cmbDictionary,Path=SelectedItem.Tag}"/>