The ComboBox - SelectionChanged event has the old value, not the new value

C #, .NET 4.0, VS2010.

New for WPF. I have a ComboBox on my MainWindow. I connected the SelectionChanged event of the specified combo box. However, if I look at the value of the combo box in the event handler, it has the old meaning. This is more like the SelectionChanging event than the SelectionChanged event.

How to get a new ComboBox value after the choice has really come?

Currently

this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged); ... private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { string text = this.MyComboBox.Text; } 

Please note: I get the same behavior if I use an object passed in event arguments, for example. e.OriginalSource.

+76
c # wpf combobox
Jun 02 '10 at 19:53
source share
16 answers

According to MSDN, e.AddedItems :

Gets a list containing the selected items.

So you can use:

 private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { string text = (e.AddedItems[0] as ComboBoxItem).Content as string; } 

You can also use SelectedItem if you use string values ​​for Items from sender :

 private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { string text = (sender as ComboBox).SelectedItem as string; } 

or

 private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { string text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string; } 

Since both Content and SelectedItem are objects, a safer approach would be to use .ToString() instead of as string

+94
Jun 02 '10 at 19:57
source share

Use the DropDownClosed event instead of selectionChanged if you want to use the current value of the combo box.

 private void comboBox_DropDownClosed(object sender, EventArgs e) { MessageBox.Show(comboBox.Text) } 

Indeed, it is simple.

+43
Apr 26 2018-11-21T00:
source share

The correct value to check here is the SelectedItem property.

ComboBox is a composite control with two parts:

  • Text part : the value in this part corresponds to the ComboBox Text property.
  • Selector part (i.e., part of the drop-down list): the selected item in this part corresponds to the SelectedItem property.

Advanced ComboBox Components

The image above was taken immediately after the ComboBox was expanded (i.e. before selecting a new value). At the moment, both Text and SelectedItem are "Information", suggesting that the ComboBox elements were strings. If the ComboBox elements were in place of all Enum values ​​called "LogLevel", the SelectedItem will now be LogLevel.Info .

When an item in the drop-down list is clicked, the Selected item value will be changed and the SelectionChanged event will be added. The Text property has not been updated yet, since the Text part is not updated until the SelectionChanged handler is completed. This can be observed by setting a breakpoint in the handler and looking at the control:

ComboBox at breakpoint in SelectionChanged handler

Since the Text part was not updated at this moment, the Text property returns the previously selected value.

+42
Jul 22 '15 at 21:43
source share

This worked for me:

 private void AppName_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBoxItem cbi = (ComboBoxItem)AppName.SelectedItem; string selectedText = cbi.Content.ToString(); } 
+10
Nov 17 '10 at 16:48
source share

This worked for me:

 private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { var text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string; } 
+4
Oct 13 '15 at 11:45
source share

The following event is fired for any text change in the ComboBox (when changing the selected index and changing the text using editing).

 <ComboBox IsEditable="True" TextBoxBase.TextChanged="cbx_TextChanged" /> 
+2
Mar 27 '17 at 9:02 on
source share

The second option did not work for me, because the .Text element was not available (C # 4.0 VS2008). That was my decision ...

 string test = null; foreach (ComboBoxItem item in e.AddedItems) { test = item.Content.ToString(); break; } 
+1
Aug 24 '10 at 21:52
source share
 private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { string newItem = ((DataRowView) e.AddedItems[0]).Row.ItemArray[0].ToString(); } 
+1
Apr 17 2018-11-12T00:
source share

I needed to solve this on VB.NET. Here's what I got:

 Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox_AllSites.SelectionChanged Dim cr As System.Windows.Controls.ComboBoxItem = ComboBox1.SelectedValue Dim currentText = cr.Content MessageBox.Show(currentText) End Sub 
0
Nov 22 '10 at 20:17
source share

It is strange that SelectedItem contains fresh data, but SelectedValue does not. Sounds like a mistake. If your elements in Combobox are objects other than ComboBoxItems, you will need something like this: (my ComboBox contains KeyValuePair s)

 var selectedItem = (KeyValuePair<string, string>?)(sender as ComboBox).SelectedItem; if (!selectedItem.HasValue) return; string selectedValue = selectedItem.Value.Value; // first .Value gets ref to KVPair 

ComboBox.SelectedItem can be null, while Visual Studio tells me that KeyValuePair cannot be null. So I applied SelectedItem to a valid KeyValuePair<string, string>? . Then I check if the SelectedItem value has a value other than null . This approach should be applicable to any type you actually choose.

0
Jul 01 '14 at 13:03
source share

If you really need the SelectionChanged event, the best answer would be SwDevMan81. However, if you are starting out with WPF, you might want to learn how to do things with WPF, which is different from the old days of Windows Forms that you used to use events like SelectionChanged , with WPF and Model View ViewModel pattern, you should use bindings . Here is a sample code:

 // In the Views folder: /Views/MyWindow.xaml: // ... <ComboBox ItemsSource="{Binding MyViewModel.MyProperties, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding MyViewModel.MyProperty , RelativeSource={RelativeSource AncestorType=Window}}" /> // ... // In the Views folder: /Views/MyWindow.xaml.cs: public partial class MyWindow : Window { public MyViewModelClass MyViewModel { get { return _viewModel; } private set { _viewModel = value;} } public MyWindow() { MyViewModel.PropertyChanged += MyViewModel_PropertyChanged; } void MyViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "MyProperty") { // Do Work // Put your logic here! } } } using System.ComponentModel; // In your ViewModel folder: /ViewModels/MyViewModelClass.cs: public class MyViewModelClass : INotifyPropertyChanged { // INotifyPropertyChanged implementation: private void NotifyPropertyChanged(string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; // Selected option: private string _myProperty; public string MyProperty { get { return _myProperty; } set { _myProperty = value; NotifyPropertyChanged("MyProperty"); } } // Available options: private List<string> _myProperties; public List<string> MyProperties { get { return _myProperties; } set { _myProperties = value; NotifyPropertyChanged("MyProperties"); } } } 
0
Jul 06 '15 at 15:13
source share
 private void indBoxProject_SelectionChanged(object sender, SelectionChangedEventArgs e) { int NewProjID = (e.AddedItems[0] as kProject).ProjectID; this.MyProject = new kProject(NewProjID); LoadWorkPhase(); } 

Using e.AddedItems[0] as kProject , where kProject is the class that contains the data processed for me, since it did not execute RemovedItems [0] by default before I made this explicit difference. Thanks to SwDevMan81 for the initial information that answered this question for me.

0
Apr 21 '16 at 14:10
source share

Do not complicate things for no reason. Using the SelectedValue property, you can easily get the selected ComboBox value, for example: YourComboBoxName.SelectedValue.ToString ().

set;} this means that you can use it to get or set the value of a ComboBox.

Using SelectedItem is not an efficient way to get a ComboBox value, as it requires a lot of consequences.

0
Jan 07 '19 at 6:32
source share
  private void comboBox_Change(object sender, SelectionChangedEventArgs e) { string item = ((sender as ComboBox).SelectedItem as TextBlock).Text; } 
0
Jan 16 '19 at 21:00
source share

This should work for you ...

 int myInt= ((data)(((object[])(e.AddedItems))[0])).kid; 
-2
Oct 24 '13 at 22:32
source share

I solved this using the DropDownClosed event because it accelerated a bit after changing the value.

-3
Jun 19 '15 at 15:45
source share



All Articles