Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding child controls to a WrapPanel

Tags:

c#

wpf

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 ?

like image 710
shanmugharaj Avatar asked Dec 03 '13 11:12

shanmugharaj


People also ask

How to use WrapPanel in Wpf?

WPF WrapPanel control is a panel that positions child elements in sequential position from left to right by default. If child elements that are stacked don't fit in the row or column they are in, the remaining elements will wrap around in the same sequence.

What is a WrapPanel?

The WrapPanel control positions child elements in sequential position from left to right, breaking content to the next line at the edge of the containing box. Subsequent ordering happens sequentially from top to bottom or from right to left, depending on the value of the Orientation property.

What is stack panel?

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.

What is a StackPanel WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.


2 Answers

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:

WideNarrow

like image 141
Somedust Avatar answered Oct 01 '22 19:10

Somedust


you should use a WrapPanel for your requirements.

like image 44
Herm Avatar answered Oct 01 '22 17:10

Herm