在WPF ComboBox中获取所选项目的标签
问题描述:
我有这样的组合框:
<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>
如何获取代码中的标签值?
编写类似 ExpireAfterTimeComboBox.SelectedItem.Tag
之类的代码无效.
writing something like ExpireAfterTimeComboBox.SelectedItem.Tag
doesn't work.
答
您需要将其强制转换为 ComboBoxItem
类型.
You need to cast it to a type of ComboBoxItem
.
var selectedTag = ((ComboBoxItem)ExpireAfterTimeComboBox.SelectedItem).Tag.ToString();