Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to cast it to a type of ComboBoxItem.

  var selectedTag = ((ComboBoxItem)ExpireAfterTimeComboBox.SelectedItem).Tag.ToString();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...