2009-03-05 7 views
0

私は自分のStackPanelにコンテンツをリダイレクトする限り得ることができました:私は、ヘッダー、その下の行を持っているコントロールを作成しようとすると、その後てるWPFで複数のアイテム/コンテンツをユーザーコントロールに許可するにはどうすればよいですか?示すように

<UserControl 
x:Name="taskItem"> 
<UserControl.ContentTemplate> 
    <DataTemplate> 
     <StackPanel> 
      <Label x:Name="labelHeader" Content="{Binding ElementName=taskItem,Path=Header}" FontFamily="Tahoma" FontSize="16" FontWeight="Bold" /> 
      <Border BorderThickness="0,1,0,0" BorderBrush="#999999" Margin="5,0,5,0"> 
       <StackPanel Margin="10,5,0,0"> 
        <ContentPresenter Content="{TemplateBinding Content}" /> 
       </StackPanel> 
      </Border> 
     </StackPanel> 
    </DataTemplate> 
</UserControl.ContentTemplate> 

N個の子コンテンツ。しかし、現在の実装では、1つ以上は許可されません。

私はここで間違っていますか?

答えて

4

ContentControlから継承しているため、定義によるユーザーコントロールには子が1つあります。ユーザーコントロールにすべてのヘッダーがあるようにし、ItemsControlをUserControlの内容にします。 DataTemplateをItemsControlのItemTemplateプロパティに適用します。

<UserControl x:Class="WindowsApplication1.UserControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
     <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Grid Name="MainHeadersGrid" Grid.Row="0"> 
     <TextBlock Text="Put your headers here" /> 
     </Grid> 

     <ItemsControl Name="childItemsWillGoInHere" ItemsSource="{Binding}" Grid.Row="1"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
      <TextBlock Text="{Binding PropertyOfItem}" /> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     </ItemsControl> 

    </Grid> 
</UserControl> 

ここで、UserControlのテンプレートのDataContextを、表示するオブジェクトのコレクションに割り当てます。

関連する問題