【发布时间】:2009-06-18 10:08:33
【问题描述】:
在下面的代码中,我通过分配 ItemTemplate 属性告诉 ComboBox 使用名为 CustomerTemplate 的 DataTemplate。
但是,StackPanel 没有 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>
【问题讨论】:
标签: wpf xaml stackpanel itemtemplate