0

My usercontrol gets loaded X amount of times and is supposed to be loaded alongside each other until there's no room. Ive tried using a wrap panel but that makes no difference and neither has a horizontal stack panel. I'm clearly doing something wrong here and would like advice:

XAML Page:

<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 Button:

<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 (calls usercontrol):

 public partial class CellNumber : Page
    {

        public CellNumber()
        {
            InitializeComponent();

            for (int i = 0; i <= Global.noCells; i++)
            {
                CellControlContainer.Items.Add(new UserControls.CellButton(i));
            }
        }
  • Current result - Buttons gets stacked vertically
  • Desired result - Buttons are wrapped horizontally
Mrparkin
  • 65
  • 10

1 Answers1

0

Use a horizontal WrapPanel or StackPanel as the ItemsControl's ItemsPanel:

<ItemsControl Name="CellControlContainer">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

Besides that, instead of adding items in code behind better assign or bind the ItemsSource property to an (optionally observable) collection of data items, and create their visual representation by a DataTemplate that is used as the ItemsControl's ItemTemplate like in this simple example:

<ItemsControl ItemsSource="{Binding CellRange}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <usercontrol:CellButton Number="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Clemens
  • 123,504
  • 12
  • 155
  • 268