Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tag of selected item in WPF ComboBox

I have combobox like this:

<ComboBox Name="ExpireAfterTimeComboBox" Margin="5" SelectedIndex="0">
    <ComboBoxItem Content="15 minutes" Tag="15" />
    <ComboBoxItem Content="30 minutes" Tag="30" />
    <ComboBoxItem Content="1 hour" Tag="60" />
    <ComboBoxItem Content="1 day" Tag="1440" />
</ComboBox>

How do I get Tag value in code?

writing something like ExpireAfterTimeComboBox.SelectedItem.Tag doesn't work.

like image 830
katit Avatar asked Jun 10 '11 16:06

katit


People also ask

What happens when we select a value from a ComboBox in WPF?

When you select an item, it will be displayed on the textbox. We recommend that you execute the above example code and try some other properties and events of the combobox control.

How do you display a value from a ComboBox in a textbox?

You can display the selected value from combobox to textbox by using the SelectedValuePath and SelectedValue property of WPF ComboBoxAdv control.


1 Answers

You need to cast it to a type of ComboBoxItem.

  var selectedTag = ((ComboBoxItem)ExpireAfterTimeComboBox.SelectedItem).Tag.ToString();
like image 169
keyboardP Avatar answered Oct 03 '22 00:10

keyboardP