StackPanel이 ItemTemplate을 사용하도록하려면 어떻게해야합니까?
-
06-07-2019 - |
문제
다음 코드에서는 말합니다 콤보 박스 할당하여 CustomerTemplate이라는 DataTemplate을 사용합니다 ItemTemplate 기인하다.
스택 패널, 그러나 itemtemplate 속성이 없습니다.
StackPanel이 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>
해결책
항목 통제 기본적으로 항목 회경이있는 스택 패널입니다. 내부적으로 스택 패널을 사용합니다.
그러나 고객 목록이 아닌 단일 고객을 표시하려고하는 것처럼 보입니다 (Clippy처럼 들리지 않습니까?). 이 경우 ContentControl을 사용하려고합니다.
<ContentControl
Content="{Binding SelectedCustomer}"
ContentTemplate="{StaticResource CustomerTemplate}" />
제휴하지 않습니다 StackOverflow