Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected value from combo box in C# WPF

Tags:

c#

combobox

wpf

I have just started using WPF forms instead of Windows Forms forms. In a Windows Forms form I could just do:

ComboBox.SelectedValue.toString(); 

And this would work fine.

How do I do this in WPF? It doesn't seem to have the option.

like image 456
Boardy Avatar asked Dec 04 '10 02:12

Boardy


People also ask

What happens when we select a value from a ComboBox in WPF?

A ComboBox control is an items control that works as a ListBox control but only one item from the collection is visible at a time and clicking on the ComboBox makes the collection visible and allows users to pick an item from the collection. Unlike a ListBox control, a ComboBox does not have multiple item selection.


2 Answers

I have figured it out a bit of a strange way of doing it compared to the old WF forms:

ComboBoxItem typeItem = (ComboBoxItem)cboType.SelectedItem; string value = typeItem.Content.ToString(); 
like image 129
Boardy Avatar answered Oct 09 '22 15:10

Boardy


Well.. I found a simpler solution.

String s = comboBox1.Text; 

This way I get the selected value as string.

like image 45
Moile Avatar answered Oct 09 '22 16:10

Moile