Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox selectionchanged event triggers without even changing the selection in the ComboBox

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

    }
like image 464
MangoTable Avatar asked Feb 16 '11 21:02

MangoTable


People also ask

Which of the following event is triggered when the selection of item in a ComboBox is changed?

The SelectionChangeCommitted event is raised only when the user changes the combo box selection, and you can create a handler for this event to provide special handling for the ComboBox when the user changes the selected item in the list.

What events would a ComboBox fire upon selecting a new item?

It records the currently selected item when the dropdown is opened, and then fires SelectionChanged event if the same index is still selected when the dropdown is closed. It may need to be modified for it to work with Keyboard selection.

What event from this class fires when the user of the control chooses an item from the ComboBox?

The combo box fires an action event when the user selects an item from the combo box's menu.

What is ComboBox in WPF?

A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.


3 Answers

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
like image 111
HCL Avatar answered Sep 20 '22 00:09

HCL


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;
}
like image 32
Arash Avatar answered Sep 20 '22 00:09

Arash


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
like image 34
dwebb Avatar answered Sep 21 '22 00:09

dwebb