用于多个控件的ComboBox.ItemTemplate

Adr*_*ica 4 silverlight xaml combobox datatemplate itemtemplate

我有10个ComboBox控件,它们将使用相同的项目模板(图像和文本块)以及相同的项目,因此我想在更全局的范围内(页面级别)定义此模板.这是我到目前为止所做的:

<UserControl.Resources>
     <DataTemplate x:Name="CBItem">
          <StackPanel Orientation="Horizontal">
              <Image Source="{Binding ImageSource}"></Image>
              <TextBlock Text="{Binding TextLabel}"></TextBlock>
          </StackPanel>
     </DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何在以下10个ComboBox控件中使用此资源.我尝试过类似的东西

        <ComboBox Height="25">
            <ComboBox.ItemTemplate>
                <DataTemplate x:Name="{StaticResource CBItem}"></DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.有帮助吗?

Ern*_*rno 7

<ComboBox Height="25" ItemTemplate="{StaticResource CBItem}"/>
Run Code Online (Sandbox Code Playgroud)

或者更好,也创造一种风格:

<Style x:Key="cmbStyle" TargetType="ComboBox">
    <Setter Property="ItemTemplate" Value="{StaticResource CBItem}" />
    <Setter Property="Height" Value="25"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

然后:

<ComboBox Style="{StaticResource cmbStyle}"/>
Run Code Online (Sandbox Code Playgroud)

或者,如果页面中的所有组合框都应具有以下样式:

<Style TargetType="ComboBox">
    <Setter Property="ItemTemplate" Value="{StaticResource CBItem}" />
    <Setter Property="Height" Value="25"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

然后:

<ComboBox />
Run Code Online (Sandbox Code Playgroud)