Reputation: 936
When I try to set the IsSelected for a ComboBoxItem it throws Set property 'IsSelected' threw an exception
. What should I do?
Here's the XAML:
<ComboBox x:Name="rowsPerPageCombo" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120" SelectionChanged="rowsPerPageCombo_SelectionChanged" Background="White">
<ComboBoxItem x:Name="Page10" Content="10" IsSelected="True"/>
<ComboBoxItem x:Name="Page20" Content="20"/>
<ComboBoxItem x:Name="Page30" Content="30"/>
<ComboBoxItem x:Name="Page40" Content="40"/>
<ComboBoxItem x:Name="Page50" Content="50"/>
</ComboBox>
Upvotes: 0
Views: 217
Reputation: 169160
It's difficult to say where your error is without having seen your code but make sure that the window has been loaded before yoy try to anything in the SelectionChanged event handler. You could return immediately if the IsLoaded property returns false:
private void rowsPerPageCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.IsLoaded)
return;
//your code...
}
If you remove SelectionChanged="rowsPerPageCombo_SelectionChanged"
and don't handle the SelectionChanged event you will probably get rid of the exception. Otherwise it is related to something else in your code.
Upvotes: 0
Reputation: 684
Try setting the SelectedIndex
property to 0 instead of setting the selected item on the ComboBoxItem
element
<ComboBox x:Name="rowsPerPageCombo" SelectedIndex="0" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120" SelectionChanged="rowsPerPageCombo_SelectionChanged" Background="White">
<ComboBoxItem x:Name="Page10" Content="10" />
<ComboBoxItem x:Name="Page20" Content="20"/>
<ComboBoxItem x:Name="Page30" Content="30"/>
<ComboBoxItem x:Name="Page40" Content="40"/>
<ComboBoxItem x:Name="Page50" Content="50"/>
</ComboBox>
Upvotes: 1