How to get ComboBox.SelectedText in WPF

In WPF, the ComboBox does not have a SelectedText property.

Is there a way to achieve the same functionality as TextBox SelectedText in WPF

+5
source share
5 answers

You can access the TextBox ComboBox using:

var edit = (TextBox)myCombo.Template.FindName("PART_EditableTextBox", myCombo);

Then you can access the SelectedText property of this TextBox:

var selectedText = edit.SelectedText;
+11
source

Since WPF is "carefree", you can display your list items in any form. there may or may not be a text element.

MyCombo.SelectedText it makes no sense if, for example, you see icons on it.

What you want is ComboBox.SelectedItemand then access to your object. for example, if you are using a backup list of People objects ....MyComboBox.SelectedItem.PersonName

, SelectedItem .

+1

There is no property called selectedText, but you can achieve this with a method ComboBox.SelectedValue.ToString(). This is because combobox values ​​are of type Object by default, so we need to output it accordingly.

0
source

Use this:

ComboBoxItem Item = (ComboBoxItem) YourComboBoxName.SelectedValue;

Then this:

Console.WriteLine(Item.Content);
0
source

Why don't you try:

string selectedtext= Combobox.Text;

This works for me.

0
source

All Articles