How can I get the StackPanel to use an ItemTemplate?

In the following code, I suggest that the ComboBox use a DataTemplate called CustomerTemplate by assigning its ItemTemplate attribute.

StackPanel , however, does not have an ItemTemplate attribute.

How can I get the StackPanel to also use the CustomerTemplate?

<Window.Resources> <DataTemplate x:Key="CustomerTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding FirstName}"/> <TextBlock Text=" "/> <TextBlock Text="{Binding LastName}"/> </StackPanel> </DataTemplate> </Window.Resources> <DockPanel LastChildFill="False" Margin="10"> <ComboBox x:Name="CustomerList" ItemTemplate="{StaticResource CustomerTemplate}" HorizontalAlignment="Left" DockPanel.Dock="Top" Width="200" SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}" ItemsSource="{Binding Customers}"/> <StackPanel DataContext="{Binding SelectedCustomer}" Orientation="Horizontal"> <TextBlock Text="Chosen: "/> <TextBlock Text="{Binding LastName}"/> </StackPanel> </DockPanel> 
+7
wpf xaml stackpanel itemtemplate
source share
1 answer

ItemsControl is essentially a StackPanel with an ItemTemplate. It uses the inner shell of the StackPanel.

However, it looks like you are trying to display a single client, not a list of them (I sound like Clippy, right?). In this case, you want to use ContentControl:

 <ContentControl Content="{Binding SelectedCustomer}" ContentTemplate="{StaticResource CustomerTemplate}" /> 
+36
source share

All Articles