Combobox selectionchanged event triggers without even changing the selection in the ComboBox
-
14-11-2019 - |
Question
Could you help me find the error in this one: The event triggers before even the windows form is loaded. I start to see the message Box and then I click OK,after that it loads the main screen.After that everything works perfectly, I wonder what triggers the ComboBox SelectionChanged Event before even loading the window.The FillComboBoxFamilyData(SegmentCode) just creates a dataset and puts the values int he ComboBox. Please Refer to this link for complete code.
Not able to make cascading comboboxes work
Any help would be highly appreciated.Thanks.
<ComboBox Height="23" HorizontalAlignment="Left" Margin="35,26,0,0" Name="comboBox1" VerticalAlignment="Top" Width="205" ItemsSource="{Binding Source={StaticResource tblSegmentViewSource}}" DisplayMemberPath="Segment Name" SelectedValuePath="Segment Code" SelectionChanged="comboBox1_SelectionChanged"/>
<ComboBox Margin="304,26,395,93" Name="comboBox2" />
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
SegmentCode = Convert.ToInt32(comboBox1.SelectedValue.ToString());
FillComboBoxFamilyData(SegmentCode);
}
Solution
At the moment the data will be loaded (attached by the binding), SelectionChanged will be fired. Therefore, you have to check in your event-handler if your app is ready and all the data is loaded and attached. If not, return the event-handler without doing anything. This behaviour is by design.
ItemsSource="{Binding Source={StaticResource tblSegmentViewSource}}"
You can use the IsLoaded
-property to detect, if the binding already has been evaluated. IsLoaded
will not be true unless the databinding-engine has evaluated your xaml-bindings.
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) {
if(!IsLoaded){
return;
}
.... your code here
OTHER TIPS
You can use IsLoaded property of the combo box to test whether it is loaded yet. This is the cleanest and easiest solution which I could find:
var comboBox = (ComboBox)sender;
if (!comboBox.IsLoaded)
{
// This is when the combo box is not loaded yet and the event is called.
return;
}
I had this same problem and I found out that setting the starting-selection-index of the combox using xaml will trigger the selectionchanged event when program is loading which causes the error.
To solve you can either set the selection-index to -1 (the default) OR change the current-selection-index of the combobox using code after the program has loaded.
I know this is an old question but I came across it twice trying to fix this in my project and had the same results as the OP. My list is populated after the IsLoaded is true. So, I figured I would post what I figured out for others. Just use the DropDowOpened event to set a bool to true. This way the SelectionChanged event won't fire until the user actually clicks on the dropdown.
private bool UserSeriesChange;
private void comboBox1_DropDownOpened(object sender, EventArgs e)
{
UserSeriesChange = true;
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(!UserSeriesChange){
return;
}
.... your code here