Динамическое добавление прямоугольников в Silverlight - PullRequest
1 голос
/ 01 марта 2011

В следующем фрагменте кода я добавляю новый прямоугольник при нажатии кнопки. В настоящее время они добавляют сверху вниз, но я хочу добавить их снизу вверх

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

Ответы [ 2 ]

2 голосов
/ 02 марта 2011

Попробуйте это.

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);
}
0 голосов
/ 01 марта 2011

Я думаю, что rect.VerticalAlignment = VerticalAlignment.Bottom и sp1.Children.Insert (0, rect) помогут вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...