当前位置: 首页 > news >正文

WPF布局面板

StackPanel

StackPanel 是一种常用的布局控件,可以支持水平或垂直排列,但不会换行。当子元素添加到 StackPanel 中时,它们将按照添加的顺序依次排列。默认情况下,StackPanel 的排列方向是垂直的,即子元素将从上到下依次排列。可以使用 Orientation 属性更改排列方向。可以设置两种排列方式:横排(Horizontal)和竖排(Vertical,默认)。水平排列时,每个元素都与面板一样高;垂直排列时,每个元素都与面板一样宽。

StackPanel 的Orientation属性

1.默认情况下Orientation="Vertical",<StackPanel Orientation="Vertical">与下方加粗字体是同一个效果 

// StackPanel 里添加了4个按钮,默认布局,Orientation 默认垂直排列
<Window x:Class="WpfFirstApp.StackPanelWindow"
        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"
        xmlns:local="clr-namespace:WpfFirstApp"
        mc:Ignorable="d"
        Title="StackPanelWindow" Height="450" Width="800">
    <StackPanel >
        <Button Content="按钮1"></Button>
        <Button Content="按钮2"></Button>
        <Button Content="按钮3"></Button>
        <Button Content="按钮4"></Button>
    </StackPanel>
</Window>

效果图:

也可不在xaml中增加button,改用代码增加;效果是一样的,C#中代码如下:

// 使用C# 代码直接实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfFirstApp
{
    /// <summary>
    /// StackPanelWindow.xaml 的交互逻辑
    /// </summary>
    public partial class StackPanelWindow : Window
    {
        public StackPanelWindow()
        {
            InitializeComponent();
            this.Title = "StackPanel面板";
            // StackPanel 默认排列
            StackPanel stackPanel = new StackPanel();
            this.Content = stackPanel;
            stackPanel.Children.Add(new Button(){Content="按钮1"});
            stackPanel.Children.Add(new Button(){Content = "按钮2"});
            stackPanel.Children.Add(new Button(){Content = "按钮3"});
            stackPanel.Children.Add(new Button(){Content = "按钮4"});
        }
    }
}

2.设置StackPanel的Orientation为横向(Horizontal)排列

// 设置 Orientation 属性(Horizontal)
<Window x:Class="WpfFirstApp.StackPanelWindow"
        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"
        xmlns:local="clr-namespace:WpfFirstApp"
        mc:Ignorable="d"
        Title="StackPanelWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal">
        <Button Content="按钮1"></Button>
        <Button Content="按钮2"></Button>
        <Button Content="按钮3"></Button>
        <Button Content="按钮4"></Button>
    </StackPanel>
</Window>

效果图:

也可不在xaml中增加button,改用代码增加;效果是一样的,C#中代码如下:

// 使用C# 代码直接实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfFirstApp
{
    /// <summary>
    /// StackPanelWindow.xaml 的交互逻辑
    /// </summary>
    public partial class StackPanelWindow : Window
    {
        public StackPanelWindow()
        {
            InitializeComponent();
            this.Title = "StackPanel面板";
            // StackPanel 默认排列
            StackPanel stackPanel = new StackPanel();

            stackPanel.Orientation = Orientation.Horizontal;
            this.Content = stackPanel;
            stackPanel.Children.Add(new Button(){Content="按钮1"});
            stackPanel.Chil

http://www.lryc.cn/news/294005.html

相关文章:

  • 灵活应对:策略模式在软件设计中的应用
  • eosio.token 智能合约介绍
  • 3D 转换
  • AI智能语音机器人安装方法
  • Python 潮流周刊#38:Django + Next.js 构建全栈项目
  • Jenkins升级后,构建任务配置界面重复错位
  • Python基础学习 -07 运算符
  • Nim游戏
  • Pytorch: torch.linspace等间隔数值函数
  • 【C++】案例:数列求和 与 条件筛选
  • 问题:下列哪些属于历史文化资源的特征( ). #学习方法#学习方法
  • 大数据 - Spark系列《四》- Spark分布式运行原理
  • Java使用规范
  • Debian 11 安装并开启SSH服务实现允许root用户使用SecureCRT远程登录
  • Linux下对线程的理解(上)
  • 【蓝桥杯】环形链表的约瑟夫问题
  • 深度学习本科课程 实验1 Pytorch基本操作
  • 大数据分析|设计大数据分析的三个阶段
  • 华为机考入门python3--(7)牛客7-取近似值
  • C# Avalonia 11.0.6 绘图
  • 使用java -jar命令运行jar包提示“错误:找不到或无法加载主类“的问题分析
  • Tomcat组件架构与数据流
  • AES算法:数据传输的安全保障
  • 前端小案例——动态导航栏文字(HTML + CSS, 附源码)
  • 前置机、堡垒机(跳板机)【2024-02-04】
  • 从编程中理解:大脑的短期记忆和长期记忆
  • Rust 本地文档的使用:rustup doc
  • uni-app切换页面刷新,返回上一页刷新(onShow钩子函数的使用)
  • adb 无线连接 操作Android设备
  • 春节运维不打烊:一体化运维高效保障企业IT与机房环境