I can't find any info on this. I think is a weird problem because it doesn't happen if I make the small app on windows form instead of WPF.
I have two radio buttons that when clicked they populate a combobox with either cats or dogs. If I use the mouse to select things everything works just fine.
The problem happens when I try to use tab to focus the combobox and then select something by typing or using the arrow keys. If I select something like that and then switch to the other radio button and try to select anything in the combobox with the mouse the combobox won't even open and the application crashes with the following error.
An unhandled exception of type 'System.NullReferenceException' occured in PresentationFramework.dll
Additional information:Object reference not set to an instance of an object.
Steps to make it crash if the above is not clear:
- Select anything from the combobox when the application starts.
- Switch to the Dogs Radio Button.
- Use tab to focus on the combobox and type D to select anything on the combobox.
- Switch back to Cats Radio Button with the mouse and try to select anything in combobox.
This is what I have.
C#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
rbCat.IsChecked = true;
}
private void rbCat_Checked(object sender, RoutedEventArgs e)
{
cbAnimals.Items.Clear();
cbAnimals.Items.Add("Cat 1");
cbAnimals.Items.Add("Cat 2");
cbAnimals.Items.Add("Cat 3");
cbAnimals.Items.Add("Cat 4");
}
private void rbDogs_Checked(object sender, RoutedEventArgs e)
{
cbAnimals.Items.Clear();
cbAnimals.Items.Add("Dog 1");
cbAnimals.Items.Add("Dog 2");
cbAnimals.Items.Add("Dog 3");
cbAnimals.Items.Add("Dog 4");
}
}
XAML
<Grid>
<RadioButton x:Name="rbCat" Content="Cats" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Checked="rbCat_Checked"/>
<RadioButton x:Name="rbDogs" Content="Dogs" HorizontalAlignment="Left" Margin="57,10,0,0" VerticalAlignment="Top" Checked="rbDogs_Checked"/>
<Label Content="Animals: " HorizontalAlignment="Left" Margin="10,31,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="cbAnimals" HorizontalAlignment="Left" Margin="73,34,0,0" VerticalAlignment="Top" Width="120"/>
</Grid>
Your help would be greatly appreciated since I am trying to write a bigger application that has a similar UI. Thanks in advance.