1

In the following code snippet I am adding new rectangle on button click. Currently they are adding from top to bottom but I want to add them in bottom to top order

<UserControl x:Class="StackImp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Canvas x:Name="LayoutRoot" Background="White">
        <Canvas x:Name="canvasChild" Background="Red"></Canvas>
        <Button x:Name="btnPush" Content="Add" Height="20" Width="40" Margin="12,268,348,12" Click="btnPush_Click"></Button>
        <Button Content="Remove" Height="20" Margin="78,268,282,12" Name="btnPop" Width="40" />
    </Canvas>
</UserControl>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace StackImp
{
    public partial class MainPage : UserControl
    {
        StackPanel sp1 = new StackPanel();
        public MainPage()
        {
            InitializeComponent();
            sp1.Orientation = Orientation.Vertical;
            canvasChild.Children.Add(sp1);

        }

        private void btnPush_Click(object sender, RoutedEventArgs e)
        {
            Rectangle rect = new Rectangle()
            {

                Height = 30,
                Width = 30,
                StrokeThickness = 3,
                Stroke = new SolidColorBrush(Colors.Red),

            };
            sp1.Children.Add(rect);
        }
    }
}
3
  • Use the Insert method instead of Add so they get added to the front. Commented Mar 1, 2011 at 8:00
  • @Hans Passant:It's not working Commented Mar 1, 2011 at 8:30
  • All the elements on the Canvas should be explicitly positioned using coordinates. If you need to aligning, may be it could be better to consider different layout elements that will better fit your needs. I don't contend that it's impossible to do with your current layout, but either way a solution will look a bit tangly. Commented Mar 1, 2011 at 9:48

2 Answers 2

2

Try this one.

XAML

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Content="Add Rectangle" Click="Button_Click" />
    <StackPanel x:Name="sp1" Grid.Row="1" VerticalAlignment="Bottom"/>
</Grid>

C#

private void Button_Click(object sender, RoutedEventArgs e)
{
    Border border = new Border();
    border.Height = 50;
    border.Width = 50;
    border.BorderBrush = new SolidColorBrush(Colors.Black);
    border.BorderThickness = new Thickness(2);
    border.Child = new TextBlock()
    {
        Text = sp1.Children.Count.ToString(),
        HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
        VerticalAlignment = System.Windows.VerticalAlignment.Center
    };
    sp1.Children.Insert(0, border);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think rect.VerticalAlignment = VerticalAlignment.Bottom and sp1.Children.Insert(0, rect) will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.