2009-06-18 176 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