wpfcomboboxbindingkeyvaluepair

ComboBox KeyValuePair Binding WPF - Display Member


i got a quick question about binding the DisplayMember of my ComboBox. I have a list with a KeyValuePair for example:

1, Value1;
2, Value2;
3, Value3;

My SelectedValuePath is set to Key which is in my example "1". Now i want my DisplayMemberPath to show "Key - Value" so for example the Textbox should show "1 - Value1". Is that possible? Thank's in advance!


Solution

  • If your ComboBox is not editable, you can create a DataTemplate for your key-value pairs.

    <ComboBox ...>
       <ComboBox.ItemTemplate>
          <DataTemplate>
             <TextBlock>
                <Run Text="{Binding Key, Mode=OneWay}"/>
                <Run Text=" - "/>
                <Run Text="{Binding Value, Mode=OneWay}"/>
             </TextBlock>
          </DataTemplate>
       </ComboBox.ItemTemplate>
    </ComboBox>