2011-12-16 5 views
4

同じアイテムテンプレート(画像とテキストブロック)と同じアイテムを使用するComboBoxコントロールが10個ありますので、このテンプレートをよりグローバルなスケール(ページレベル)で定義したいのですが、 。これは私がこれまで何をやったかである:複数のコントロールのComboBox.ItemTemplate

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

問題は、私は、次の10のComboBoxコントロールで、このリソースを使用する方法がわからないということです。私は何かを試しました

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

しかし、それは動作しません。どんな助け?

答えて

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

またはそれ以上は、また、スタイルを作成:

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

、その後:

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

または、ページ内のすべてのコンボボックスは、このスタイルを持っている必要がある場合:

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

、次に:

<ComboBox /> 
+0

ロック!ありがとう! –

関連する問題