2009-06-18 173 views
7

在以下代码中,我通过分配其ItemTemplate属性告诉组合框使用名为CustomerTemplate的DataTemplate。我怎样才能让StackPanel使用ItemTemplate?

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> 

回答

36

ItemsControl基本上是与一个ItemTemplate一个StackPanel。它在内部使用StackPanel。

然而,它看起来像你试图显示一个客户,而不是他们的名单(我听起来像Clippy,不是吗?)。在这种情况下,要使用一个ContentControl中:

<ContentControl 
    Content="{Binding SelectedCustomer}" 
    ContentTemplate="{StaticResource CustomerTemplate}" /> 
+1

完美,另一个有用的控制爬出来的木工,感谢 – 2009-06-18 11:05:19