My goal is to have a ComboBox
where every drop down item has a specific text and a specific item associated with it so that, for example, if some clicks "blah" - the selected item will be 3.
As far as I can see - there's only one "Content" which represents both the Text and the Value. So how do I get both separately? (In either XAML or code, but without Binding.)
It is highly recommenced to use binding with XAML controls, however, you can define ComboBox items in XAML by the Items property:
<ComboBox x:Name="comboBox1"
SelectionChanged="ComboBox_SelectionChanged"
SelectedValuePath="Tag">
<ComboBox.Items>
<ComboBoxItem Tag="1">Item 1</ComboBoxItem>
<ComboBoxItem Tag="2">Item 2</ComboBoxItem>
<ComboBoxItem Tag="3">Item 3</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
and get selected items in code:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
Debug.WriteLine(comboBox1.SelectedValue);
}
Since the ComboBox item class has not a Value property, you can use the tag property to hold the corresponding value. Settings the SelectedValuePath property tells the ComboBox which property to use as Value.