C# 如何向包装中添加多个元素?

C# 如何向包装中添加多个元素?,c#,wpf,C#,Wpf,我试图在WrapPanel中插入几个StackPanel,您可以看到下面的XAML: 只有StackPanel内的TextBlock会被修改,以免重复标题和文本 <Window x:Class="ambient_test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://s

我试图在
WrapPanel
中插入几个
StackPanel
,您可以看到下面的XAML:

只有
StackPanel
内的
TextBlock
会被修改,以免重复标题和文本

<Window x:Class="ambient_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ambient_test"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ScrollViewer CanContentScroll="True">
            <WrapPanel x:Name="wrappanel">
                <StackPanel x:Name="panel1" Width="120" Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="120"/>
                            <RowDefinition Height="50"/>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0" Background="#FF38C59F"></StackPanel>
                        <TextBlock Grid.Row="2" Text="Title 1" Foreground="LightGray" VerticalAlignment="Top" TextAlignment="Center"/>
                        <TextBlock Grid.Row="1" Text="Text 1" Foreground="#FF747474" TextAlignment="Center" Margin="0 15 0 0"/>
                    </Grid>
                </StackPanel>
                <StackPanel x:Name="panel2" Width="120" Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="120"/>
                            <RowDefinition Height="50"/>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0" Background="#FF38C59F"></StackPanel>
                        <TextBlock Grid.Row="2" Text="Title 2" Foreground="LightGray" VerticalAlignment="Top" TextAlignment="Center"/>
                        <TextBlock Grid.Row="1" Text="Text 2" Foreground="#FF747474" TextAlignment="Center" Margin="0 15 0 0"/>
                    </Grid>
                </StackPanel>
            </WrapPanel>
        </ScrollViewer>
    </Grid>
</Window>
例如,我的
List
有100条记录,因此我需要在我的
WrapPanel
中添加100条
StackPanel
,与列表的标题和文本相同


有没有办法做到这一点?例如,使用绑定?

您可以使用
ItemsControl
,该控件使用
WrapPanel
作为其
ItemsPanel
,并通过适当的
DataTemplate
定义项目的布局:

<ItemsControl x:Name="itemsControl">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="120"/>
                    <RowDefinition Height="50"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Text="{Binding Title}" .../>
                <TextBlock Grid.Row="1" Text="{Binding Text}" .../>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

您可以使用
ItemsControl
,该控件使用
WrapPanel
作为其
ItemsPanel
,并通过适当的
DataTemplate
定义项目的布局:

<ItemsControl x:Name="itemsControl">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="120"/>
                    <RowDefinition Height="50"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Text="{Binding Title}" .../>
                <TextBlock Grid.Row="1" Text="{Binding Text}" .../>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
itemsControl.ItemsSource = infos;