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

Categories

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()

it returns this:

System.Windows.Controls.ComboBoxItem: Text2

How can I get a selected text or value?

See Question&Answers more detail:os

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

1 Answer

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

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

Or for Tag:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...