locked
Combo Box automatically select the first value annoyingly RRS feed

  • Question

  • I have a Combo Box whose ItemsSource is linked to a Domain Source's Data. But everytime after loading, the first item is automatically selected. I do not want anything to be selected! I tried to set ComboBox.SelectedIndex = -1 in Loaded event of the ComboBox, but every time it's set to -1, it'll automatically set back to 0. I don't know what is causing this, and how to fix???


    Help. Thanks!!!

    Wednesday, December 1, 2010 7:28 PM

Answers

  • Hi,

    You can try bind data to ComboBox in ComboBox_DropDownOpen event.

    Then when loaded, there is no resource in ComboBox, so there will be no item seleted.

     

    Best Regards,

    Thursday, December 9, 2010 5:30 AM
  • Try binding the SelectedValue property of the combobox to some value (if you dont intend to use it, no matter, just bind it to a property named Ignored or something).

    If that value doesnt exist in the combo-box, you should get an empty combobox and no selectedIndex value. This happens for me with a standard ComboBox in a couple of projects...

    Thursday, December 9, 2010 6:58 AM

All replies

  • Did you try to set the SelectedItem to null?  You can also try to set the SelectedIndex after the data is loaded.

    Wednesday, December 1, 2010 7:31 PM
  • that's what i did/said. ComboBox.SelectedIndex = -1 in loaded event won't work. Afterward, it's still automatically set back to 0!

    Wednesday, December 1, 2010 7:42 PM
  • Here are my codes. The combo box keep choosing first item as selected automatically, driving me nuts...I want it to select nothing. 



           
    private void CostCenterComboBox_Loaded(object sender, RoutedEventArgs e)
            {
                CostCenterComboBox.ItemsSource = null;
                payroll_CostCenterDomainDataSource.DomainContext.EntityContainer.Clear();
                payroll_CostCenterDomainDataSource.Load();
                
            }
    private void payroll_CostCenterDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e) { if (e.HasError) { System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK); e.MarkErrorAsHandled(); } else { CostCenterComboBox.ItemsSource = payroll_CostCenterDomainDataSource.DataView; payroll_CostCenterDomainDataSource.DataView.MoveCurrentToPosition(-1); CostCenterComboBox.SelectedIndex = -1; } }

    in .xaml:

    <ComboBox Height="23" Name="CostCenterComboBox" Width="120" Loaded="CostCenterComboBox_Loaded" />
                                    
    <riaControls:DomainDataSource AutoLoad="False" d:DesignData="{d:DesignInstance my1:Payroll_CostCenter, CreateList=true}" Height="0" LoadedData="payroll_CostCenterDomainDataSource_LoadedData" Name="payroll_CostCenterDomainDataSource" QueryName="GetPayroll_CostCenterQuery" Width="0">
                                        <riaControls:DomainDataSource.DomainContext>
                                            <my:CostCenterDomainContext />
                                        </riaControls:DomainDataSource.DomainContext>
                                    </riaControls:DomainDataSource>




    Thursday, December 2, 2010 3:18 PM
  • Hi,

    You can try bind data to ComboBox in ComboBox_DropDownOpen event.

    Then when loaded, there is no resource in ComboBox, so there will be no item seleted.

     

    Best Regards,

    Thursday, December 9, 2010 5:30 AM
  • Try binding the SelectedValue property of the combobox to some value (if you dont intend to use it, no matter, just bind it to a property named Ignored or something).

    If that value doesnt exist in the combo-box, you should get an empty combobox and no selectedIndex value. This happens for me with a standard ComboBox in a couple of projects...

    Thursday, December 9, 2010 6:58 AM