繁体   English   中英

如何在 WPF 中为用户控件允许多个项目/内容?

[英]How do I allow multiple items/content for a user control in 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>

我正在尝试创建一个控件,它有一个 header,它下面有一行,然后是 N 个子内容。 但是,在当前的实现中,它不允许超过一个。

我在这里做错了什么?

根据定义,用户控件有一个子控件,因为它继承自 ContentControl。 使用户控件具有所有标题,然后使 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 分配给要显示的对象的集合。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM