Combobox selectionchanged event triggers without even changing the selection in the ComboBox
Asked Answered
M

5

8

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);

    }
Misstate answered 16/2, 2011 at 21:16 Comment(1)
did you write the code, private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e), or was that produced by a SDK?Caracalla
M
17

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
Medicament answered 16/2, 2011 at 21:28 Comment(2)
Thank you HCL for the response.Misstate
Thank you HCL for the response.I tried putting the code in the SelectionChanged event and it didn't work, the reason I think is because it gets to the code inside the comboBoxSelection event after the event is fired, by that time data is already loaded,so it moves on and executes the code. we have to stop the selectionChanged event from executing while windows is being loaded.I wonder why it even triggers when the selection in the combobox is not even being changed?Misstate
E
4

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;
}
Epoch answered 12/10, 2016 at 22:28 Comment(0)
A
2

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
Advertisement answered 22/5, 2016 at 22:55 Comment(1)
There is another question that's going around with the same thing (you can find it here). It basically has the same approach. But +1 because I stumbled on it here and I found your answer. thanks :)Alluvion
P
1

You can add the following code to make select-change work when the drop-down list is expanded

       if (sender is ComboBox comboBox)
        {
            if (comboBox.IsDropDownOpen)
            {
                var comboBoxSelectedItem = comboBox.SelectedItem;
            }
        }
Pino answered 16/11, 2023 at 6:41 Comment(0)
H
0

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.

Hegemony answered 13/6, 2013 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.