开发者

Add rectangle controls dynamically to silverlight

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);
        }
    }
}


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);
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜