WPF控件-StackPanel控件

本文介绍了WPF中StackPanel控件的使用方法,演示了如何通过该控件实现文本块的竖直排列,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值