Reputation: 1050
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.
Upvotes: 0
Views: 811
Reputation: 1050
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?
Upvotes: 0