我的用户控件被加载X次,并且应该并排加载,直到没有空间为止。香港专业教育学院试图使用包装板,但没有什么区别,也没有水平堆叠板。我显然在这里做错了,并希望提供建议:
XAML页面:
<Grid Background="#FFFFFF">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" x:Name="lblCellPageTitle" Content="Cellect" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" Height="43" VerticalAlignment="Top" Width="221" FontSize="22"/>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<ItemsControl Grid.Row="1" Name="CellControlContainer"/>
</StackPanel>
</Grid>
用户控制按钮:
<UserControl x:Class="WpfApp1.UserControls.CellButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="180">
<Button Name="Cellx" Content="CellX" Width="100" Height="100" FontSize="19" Padding="5" Click="Cellx_Click" />
</UserControl>
Page.cs(调用用户控件):
public partial class CellNumber : Page
{
public CellNumber()
{
InitializeComponent();
for (int i = 0; i <= Global.noCells; i++)
{
CellControlContainer.Items.Add(new UserControls.CellButton(i));
}
}
答案 0 :(得分:0)
使用水平WrapPanel或StackPanel作为ItemsControl的ItemsPanel
:
<ItemsControl Name="CellControlContainer">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
此外,与其在代码中添加项目,不如在后面更好地将ItemsSource
属性分配或绑定到(可选的可观察的)数据项集合,并通过用作ItemControl的{ {1}}就像这个简单的例子一样:
ItemTemplate