0

I want a list of items to appear in my combobox, each item should be an image followed by some text. I am adding 1 item at the moment to my list (I previously held it as an observiablecollection but same problem).

Here is my combobox:

<ComboBox Grid.Row="0" ItemsSource="{Binding CrewComboSource}" SelectedValue="{Binding Crew01S}" Margin="2,0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Image}" Width="30"/>
                <TextBlock Text="{Binding Name}" FontStyle="Bold"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

I have a class here:

class CrewCombo
{
    public string Name
    {
        get;
        set;
    }

    public string Image
    {
        get;
        set;
    }
}

And initialising stuff here:

CrewComboSource = new List<CrewCombo>();
CrewCombo tempCrewMember = new CrewCombo();
tempCrewMember.Name = "test";
tempCrewMember.Image = AppDomain.CurrentDomain.BaseDirectory + "Media\\POP\\Wisp.png";
CrewComboSource.Add(tempCrewMember);

The image displays correctly when using:

<Image Grid.Row="1" HorizontalAlignment="Center" Source="{Binding CrewComboSource[0].Image}" Width="30" />

Exception info:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.

An unhandled exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll

Additional information: Object reference not set to an instance of an object.

If I edit my XAML, such that the image and textblock within the stackpanel in datatemplate are non existent, so it is open stackpanel, comment, comment, end stackpanel then I don't get this issue (but obviously I don't get anything in the combobox either). De-commenting EITHER the textblock or the image reintroduces the exception.

pingu2k4
  • 854
  • 11
  • 30
  • 1
    Can you add the full exception text and stack trace? – user700390 Mar 19 '15 at 14:58
  • Added. I don't have any more info than that because its "external code"? – pingu2k4 Mar 19 '15 at 15:08
  • You didn't add `tempCrewMember` to your list... – Patrick Hofman Mar 19 '15 at 15:09
  • well I think op didn't write it .. but he is adding as he can see image – Muds Mar 19 '15 at 15:12
  • I was able to add tempCrewMember, as the image is showing perfectly in the image tag used elsewhere. Also, when looking at the variables, it is added. public List CrewComboSource { get; set; } – pingu2k4 Mar 19 '15 at 15:13
  • no u didn't gave us code where u add this object – Muds Mar 19 '15 at 15:22
  • I forgot to copy paste the last line, but it is added in now. I am adding it to the list honestly. – pingu2k4 Mar 19 '15 at 15:32
  • @PatrickHofman A `NullReferenceException` deep within the bowels of WPF is not the same as not understanding a `NullReferenceException`. This question should not have been hastily closed. – Ryan Emerle Mar 19 '15 at 15:38
  • @MatthewParker You might want to try [breaking on unhandled exceptions](https://msdn.microsoft.com/en-us/library/038tzxdw.aspx) as this might provide more information. Also, [this question](http://stackoverflow.com/questions/12833294/provide-value-on-system-windows-baml2006-typeconvertermarkupextension-threw-a) may be similar. – Ryan Emerle Mar 19 '15 at 15:39
  • @RyanEmerle: It wasn't. The line `CrewComboSource.Add(tempCrewMember)` wasn't there yet, so the `CrewComboSource[0]` failing isn't a big surprise then. – Patrick Hofman Mar 19 '15 at 15:44
  • I provided a bit more info at the bottom. Hiding the textblock and image in comments in XAML stops the exception... the image with CrewComboSource[0] has always displayed fine. The image and textblock inside the combobox are what's causing the problem... – pingu2k4 Mar 19 '15 at 15:45
  • I am finding the following highly susicious: SelectedValue="{Binding Crew01S}" I just don't see where Crew01S is defined in the code. Are you trying this in a WYSIWYG editor and does it show any errors there at design time? – user700390 Mar 19 '15 at 16:00

1 Answers1

0

I don't know what fixed it... But I rewrote the XAML by hand, 1 step at a time and it now works fine... Its strange. I think the XAML I ended up with is identical???

<ComboBox Grid.Row="0" ItemsSource="{Binding CrewComboSource}" SelectedValue="{Binding Crew01S}" Margin="2,0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Image}" Width="30"/>
                <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Anyways, it now works correctly... No idea why. :/ Would still be interested in hearing why this might have happened?

pingu2k4
  • 854
  • 11
  • 30
  • The XAML is identical, it's impossible that this _fixed_ your issue. You must have changed something somewhere else. – Mike Eason Mar 19 '15 at 16:04