4

I have 10 ComboBox controls that will use the same item template (an Image and a Textblock), and the same items, so I want to define this template on a more global scale (page level). This is what I've done so far:

<UserControl.Resources>
     <DataTemplate x:Name="CBItem">
          <StackPanel Orientation="Horizontal">
              <Image Source="{Binding ImageSource}"></Image>
              <TextBlock Text="{Binding TextLabel}"></TextBlock>
          </StackPanel>
     </DataTemplate>
</UserControl.Resources>

The problem is that I don't know how to use this resource in the following 10 ComboBox controls. I've tried something like

        <ComboBox Height="25">
            <ComboBox.ItemTemplate>
                <DataTemplate x:Name="{StaticResource CBItem}"></DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

But it doesn't work. Any help?

Adrian Marinica
  • 2,191
  • 5
  • 29
  • 53

1 Answers1

7
<ComboBox Height="25" ItemTemplate="{StaticResource CBItem}"/>

Or better, also create a style:

<Style x:Key="cmbStyle" TargetType="ComboBox">
    <Setter Property="ItemTemplate" Value="{StaticResource CBItem}" />
    <Setter Property="Height" Value="25"/>
</Style>

and then:

<ComboBox Style="{StaticResource cmbStyle}"/>

Or, if all the Comboboxes in the page should have this style:

<Style TargetType="ComboBox">
    <Setter Property="ItemTemplate" Value="{StaticResource CBItem}" />
    <Setter Property="Height" Value="25"/>
</Style>

and then:

<ComboBox />
Emond
  • 50,210
  • 11
  • 84
  • 115