3

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:

  1. Select anything from the combobox when the application starts.
  2. Switch to the Dogs Radio Button.
  3. Use tab to focus on the combobox and type D to select anything on the combobox.
  4. 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.

NCreature
  • 49
  • 4
  • Good question. Looks like a bug. Will wait for answers from some .net experts. – ViVi Aug 18 '16 at 03:50
  • You should be awarded a bounty from Microsoft by identifying such bug in their framework. – Umair Farooq Aug 18 '16 at 03:59
  • 3
    I checked the exception stack, and it is probably this bug https://connect.microsoft.com/VisualStudio/feedback/details/1660886/system-windows-controls-combobox-coerceisselectionboxhighlighted-bug. Will be fixed in 4.6.2. (Last line of call stack was at `ComboBox.CoerceIsSelectionBoxHighlighted(Object o, Object value)`.) – aksu Aug 18 '16 at 05:16
  • 2
    More discussion here http://stackoverflow.com/questions/29306589/crash-in-combobox-coerce-not-my-code – aksu Aug 18 '16 at 05:19
  • Good to know. Thank you so much. – NCreature Aug 18 '16 at 05:50

0 Answers0