无法在ComboBox中获得选定的文本或值 [英] Cannot get a selected text or value in ComboBox

查看:65
本文介绍了无法在ComboBox中获得选定的文本或值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的wpf应用程序中有一个包含三个项目的combox:

I have a combox in my wpf application with three items:

<ComboBoxItem Tag="some value">Text</ComboBoxItem>
<ComboBoxItem Tag="some value2">Text2</ComboBoxItem>
<ComboBoxItem Tag="some value3">Text3</ComboBoxItem>

我想在运行时获取选定的文本或值.当我这样做时:

I want to get a selected text or value at runtime. When I do this:

myComboBox.SelectedValue.ToString()

它返回此:

System.Windows.Controls.ComboBoxItem: Text2

如何获取选定的文本或值?

How can I get a selected text or value?

推荐答案

因为您想要 ComboBoxItem Content 属性,因此应尝试以下操作:

Because you want the Content property of ComboBoxItem you should try like this:

(myComboBox.SelectedValue as ComboBoxItem).Content.ToString();

或用于标记:

(myComboBox.SelectedValue as ComboBoxItem).Tag.ToString();

这篇关于无法在ComboBox中获得选定的文本或值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆