Adding child controls to a WrapPanel

c#, wpf

Solution

XAML:

<Window x:Class="WpfTestBench.PanelSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PanelSample" Height="300" Width="300">
    <Grid>
        <WrapPanel Name="MyPanel" />
    </Grid>
</Window>

Codebehind:

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfTestBench
{
    public partial class PanelSample
    {
        public PanelSample()
        {
            InitializeComponent();

            for (var i = 0; i < 5; i++)
            {
                MyPanel.Children.Add(new Rectangle
                {
                    Width = 100,
                    Height = 20,
                    StrokeThickness = 1,
                    Stroke = new SolidColorBrush(Colors.Black),
                    Margin = new Thickness(5)
                });
            }
        }
    }
}

Execution result:

Problem

I am very much new to WPF. I have a very simple problem. I have a stackpanel spTerminalBox. ``` <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="300"/> <ColumnDefinition Width="881*"/> <ColumnDefinition Width="11*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="156"/> <RowDefinition Height="371*"/> </Grid.RowDefinitions> <my:WindowHeader x:Name="title" Title="Internet Cafe management software (ICM)" CloseClicked="window_CloseClicked" VerticalAlignment="Top" Margin="0,-1,0,0" Grid.ColumnSpan="3" /> <StackPanel Name ="spTerminalBox" Grid.Column="1" Grid.Row="1" Orientation="Horizontal" Margin="10,10,10,20"/> </Grid> ``` My xaml structure is that. I am filling a user control in that stackpanel dynamically in code. Once if child elements on a StackPanel do not fit in the StackPanel area, then it should not go outside of the visible area, it should come down. How to achieve this ?

Original source