关闭

WPF控件-StackPanel控件

629人阅读 评论(0) 收藏 举报
分类:

StackPanel元素用于水平或垂直堆叠子元素,StackPanel 要么垂直叠放包含的控件,要么将包含的控件排列在水平行中,具体情况取决于 Orientation 属性的值。 如果将比 StackPanel 的宽度能显示的控件还要多的控件添加到 StackPanel 中,这些控件将被截掉且不显示。 (MSDN)

下面以两个Demo练习一下:

1:

<Window x:Class="Demo_StackPanel.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="送元二使安西" Height="350" Width="525">
    <StackPanel>
        <TextBlock HorizontalAlignment="Center" FontSize="16" Foreground="Brown">渭城朝雨邑轻尘</TextBlock>
        <TextBlock HorizontalAlignment="Center" FontSize="16" Foreground="Brown">客舍青青柳色新</TextBlock>
        <TextBlock HorizontalAlignment="Center" FontSize="16" Foreground="Brown">劝君更尽一杯酒</TextBlock>
        <TextBlock HorizontalAlignment="Center" FontSize="16" Foreground="Brown">西出阳关无故人</TextBlock>
        <Button Background="Coral"  Click="Onvertical">竖排</Button>
    </StackPanel>
</Window>
2:

<Window x:Class="Demo_StackPanel.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
       
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Demo_StackPanel
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            this.Title = "送元二使安西";
            StackPanel sp = new StackPanel();
            this.Content = sp;
            sp.Children.Add(CreateTextBlock("渭城朝雨邑轻尘"));
            sp.Children.Add(CreateTextBlock("客舍青青柳色新"));
            sp.Children.Add(CreateTextBlock("劝君更尽一杯酒"));
            sp.Children.Add(CreateTextBlock("西出阳关无故人"));

            Button btn = new Button();
            btn.Height = 40;
            btn.Background = new SolidColorBrush(Colors.Brown);
            btn.Click += new RoutedEventHandler(btn_Click);
            btn.Content = "竖排";
            sp.Children.Add(btn);
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
          
        }
       
        //定义方法,根据诗句创建TextBlock
        private TextBlock CreateTextBlock(string text)
        {
            TextBlock tb = new TextBlock();
            tb.HorizontalAlignment = HorizontalAlignment.Center;
            tb.FontSize = 16;
            tb.Foreground = new SolidColorBrush(Colors.BlueViolet);
            tb.Text = text;
            return tb;
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:464236次
    • 积分:5827
    • 等级:
    • 排名:第4379名
    • 原创:16篇
    • 转载:634篇
    • 译文:0篇
    • 评论:21条
    最新评论