5

Adding child controls to Stackpanel WPF C #

 2 years ago
source link: https://www.codesd.com/item/adding-child-controls-to-stackpanel-wpf-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Adding child controls to Stackpanel WPF C #

advertisements

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 ?


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:

wCkF4.jpg
IU00M.jpg
Tags wpf

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK