ComboBox.ItemTemplate for multiple controls

I have 10 ComboBox controls that will use the same element template (image and text block) and the same elements, so I want to define this template on a more global scale (page level). This is what I have 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 next 10 ComboBox controls. I tried something like

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

But that will not work. Any help?

+4
source share
1 answer
 <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 Comboboxes on the page should have this style:

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

and then:

 <ComboBox /> 
+7
source

Source: https://habr.com/ru/post/1386691/


All Articles