Category: Combobox

Issue with Silverlight Combobox SelectionChange event

We have a Silverlight Combobox and if there is a Silverlight SelectionChange event registered to it (in XAML) in that case the event is fired even when the first time Silverlight Application is loaded.


private void cmbSelection_SelectionChanged(object sender , SelectionChangedEventArgs e)
{
MessageBox.Show("Event Fired");
}

Above event will be fired and will show when the Application will load for the first time.

Other issue that you might face is Using
if(cmbSelection.SelectedIndex ==0)
{
}
in your event will throw exception.

The cause of above issue is that event is fired before the values for the combobox are initialized and the value for cmbSelection.SelectedIndex is null.

For resolving above both issues you can register the even after everything is initialized.
public MainPage()
{
InitializeComponent();
cmbSelection.SelectionChanged += new SelectionChangedEventHandler(cmbSelection_SelectionChanged);
}