I have just started using WPF forms instead of Windows Forms forms. In a Windows Forms form I could just do:
ComboBox.SelectedValue.toString();
I had a similar issue and tried a number of solutions suggested in this thread but found that the SelectionChanged Event was firing before the ComboBox item had actually updated to show the new selection (i.e. so it always gave the contents of the combobox prior to the change occurring).
In order to overcome this, I found that it was better to use the e parameter that is automatically passed to the event handler rather than trying to load the value directly from the combo box.
XAML:
<Window.Resources>
<x:Array x:Key="Combo" Type="sys:String">
<sys:String>Item 1</sys:String>
<sys:String>Item 2</sys:String>
</x:Array>
</Window.Resources>
<Grid>
<ComboBox Name="myCombo" ItemsSource="{StaticResource Combo}" SelectionChanged="ComboBox_SelectionChanged" />
<TextBlock Name="MyTextBlock"></TextBlock>
</Grid>
C#:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string chosenValue = e.AddedItems[0].ToString();
}
Solving this problem is simple. All I did was to add "SelectedValuePath" to my XAML code and bind it to my model property that I want to return with the combobox.
<ComboBox SelectedValuePath="_Department"
DisplayMemberPath="_Department"
Height="23"
HorizontalAlignment="Left"
ItemsSource="{Binding}"
Margin="-58,1,0,5"
Name="_DepartmentComboBox"
VerticalAlignment="Center"
Width="268"/>
This largely depends on how the box is being filled. If it is done by attaching a DataTable
(or other collection) to the ItemsSource
, you may find attaching a SelectionChanged
event handler to your box in the XAML and then using this in the code-behind useful:
private void ComboBoxName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cbx = (ComboBox)sender;
string s = ((DataRowView)cbx.Items.GetItemAt(cbx.SelectedIndex)).Row.ItemArray[0].ToString();
}
I saw 2 other answers on here that had different parts of that - one had ComboBoxName.Items.GetItemAt(ComboBoxName.SelectedIndex).ToString();
, which looks similar but doesn't cast the box to a DataRowView
, something I found I needed to do, and another: ((DataRowView)comboBox1.SelectedItem).Row.ItemArray[0].ToString();
, used .SelectedItem
instead of .Items.GetItemAt(comboBox1.SelectedIndex)
. That might've worked, but what I settled on was actually the combination of the two I wrote above, and don't remember why I avoided .SelectedItem
except that it must not have worked for me in this scenario.
If you are filling the box dynamically, or with ComboBoxItem
items in the dropdown directly in the XAML, this is the code I use:
private void ComboBoxName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cbx = (ComboBox)sender;
string val = String.Empty;
if (cbx.SelectedValue == null)
val = cbx.SelectionBoxItem.ToString();
else
val = cboParser(cbx.SelectedValue.ToString());
}
You'll see I have cboParser
, there. This is because the output from SelectedValue
looks like this: System.Windows.Controls.Control: Some Value
. At least it did in my project. So you have to parse your Some Value
out of that:
private static string cboParser(string controlString)
{
if (controlString.Contains(':'))
{
controlString = controlString.Split(':')[1].TrimStart(' ');
}
return controlString;
}
But this is why there are so many answers on this page. It largely depends on how you are filling the box, as to how you can get the value back out of it. An answer might be right in one circumstance, and wrong in the other.
I use this code, and it works for me:
DataRowView typeItem = (DataRowView)myComboBox.SelectedItem;
string value = typeItem.Row[0].ToString();
private void usuarioBox_TextChanged(object sender, EventArgs e)
{
string textComboBox = usuarioBox.Text;
}
if you want to get the value and validate it you can do something like this
string index = ComboBoxDB.Text;
if (index.Equals(""))
{
MessageBox.Show("your message");
}
else
{
openFileDialog1.ShowDialog();
string file = openFileDialog1.FileName;
reader = new StreamReader(File.OpenRead(file));
}