I can not install ContentTemplate for ComboBoxItem. There is a reason why I am trying to do this - I want to have two entries for my data in the combo box. When the combo box is open (the menu is omitted), I want a text box (with the image name) and an image control under it. When I select an item, I want the combo box to display only the text box with the image name.
I thought I could achieve this by changing the ItemTemplate and ItemContainerStyle from ComboBox. ItemContainerStyle contains the following ContentPresenter:
<ContentPresenter HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
So, I assumed that I could just install the ContentTemplate here, and that would work. But I can not get it to work:
<DataTemplate x:Key="ComboBoxDataTemplate"> <Grid> <TextBlock Text="{Binding Path='Name'}"/> </Grid> </DataTemplate> <DataTemplate x:Key="ComboBoxItemTemplate"> <StackPanel> <TextBlock Text="{Binding Path='Name'}"/> <Image Source="{Binding Path='Source'}" Width="64" Height="64"/> </StackPanel> </DataTemplate> <Style x:Key="ComboBoxItemStyle1" TargetType="ComboBoxItem"> ... <Setter Property="ContentTemplate" Value="{StaticResource ComboBoxItemTemplate}"/> ...
Here is my combo box:
<ComboBox Width="70" Margin="3,0,0,0" ItemsSource="{StaticResource Source}" ItemTemplate="{StaticResource ComboBoxDataTemplate}" ItemContainerStyle="{StaticResource ComboBoxItemStyle1}" />
The only way to get this to work is to remove the ContentPresenter from the ItemContainerStyle and replace it with the contents of my custom template (ComboBoxItemTemplate). But I didn’t think I should use this approach, as this means that ContentPresenter no longer exists (and the code in ComboBox can rely on it to exist).
Any help on showing the combined block with another drop down and selected templates is welcome!
Mark ingram
source share