WPF之布局

1、StackPanel布局作用:依次排列,默认垂直排序,里面有个属性Orientation可以控制是水平依次排序还是垂直依次排序

2、Grid:表格布局

默认情况下是一行一列的grid,我们可以动态分配多行多列,来达到我们的布局效果

行列是可以动态增加的,元素是可以归属于某一行列的

布局中的宽度和高度,除了通过Height= "60"直接赋值外,还可以按照比例分配,例如3*,表示的是按照比例为3来自动分配,还可以通过设置AUTO,如:Height= "AUTO",表示根据里面内容,自动调整;

3、网格布局,设置多行,下面一个设计了三行 ShowGridLines="true"属性能够显示网格布局的线条

<Grid.RowDefinitions>

<RowDefinition Height="40"/>

<RowDefinition/>

<RowDefinition/>

</Grid.RowDefinitions>

下面代码,用于向网格布局中增加控件,控件位置可以选择任意行列

<Button Grid.Column="0" Width="40" Height="40" HorizontalAlignment="Left" />

效果图:

对应的xmal文件:【重要思想:通过grid进行模块分层设计,通过stackPanel进行自动依次排序】

cs 复制代码
<Window x:Class="Clock.MainWindow"
        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:Clock"
        mc:Ignorable="d"
        Title="入门WPF" Height="450" Width="800"  WindowStartupLocation="CenterScreen">

    <Grid ShowGridLines="true" >
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
            <RowDefinition/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Column="0" Grid.Row="0" Orientation="Horizontal"  >
            <Button Height="25" Width="70" Content="文件"/>
            <Button Height="25" Width="70" Content="编辑"/>
            <Button Height="25" Width="70" Content="查看"/>
            <Button Height="25" Width="70" Content="外观"/>
            <Button Height="25" Width="70" Content="设置"/>
            <Button Height="25" Width="70" Content="帮助"/>
        </StackPanel>

        <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal"  >
            <Button Height="25" Width="30" Content="1"/>
            <Button Height="25" Width="30" Content="2"/>
            <Button Height="25" Width="30" Content="3"/>
            <Button Height="25" Width="30" Content="4"/>
            <Button Height="25" Width="30" Content="5"/>
            <Button Height="25" Width="30" Content="6"/>
        </StackPanel>
        <!--第三行加入grid布局,然后在grid里再增加两列-->
        <Grid Grid.Row="2" Grid.Column="0" ShowGridLines="true" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"/>
                <ColumnDefinition />  
            </Grid.ColumnDefinitions>

            <StackPanel Grid.Row="0" Grid.Column="0">
                <Button Height="30" Width="40" Content="1"/>
                <Button Height="30" Width="40" Content="2"/>
                <Button Height="30" Width="40" Content="3"/>
                <Button Height="30" Width="40" Content="4"/>
                <Button Height="30" Width="40" Content="5"/>
             
            </StackPanel>
            <TextBox Grid.Row="0" Grid.Column="1" TextWrapping="Wrap"/>
        </Grid>
        <!--<Button Grid.Column="0" Grid.Row="0" Width="40" Height="40" HorizontalAlignment="Left"/>
        <Button Grid.Column="0" Width="40" Height="40" HorizontalAlignment="Left" Margin="53,0,0,0"/>-->

    </Grid>
</Window>
相关推荐
曹天骄5 小时前
Cloudflare KV 使用教程(基于 Wrangler 项目)
wpf
摘星编程9 小时前
Flutter for OpenHarmony 实战:Dialog 对话框详解
flutter·wpf
ou.cs10 小时前
WPF TreeView 自动展开所有节点:附加行为(Attached Behavior)保姆级实现教程
c#·.net·wpf
一念春风11 小时前
可视化视频编辑(WPF C#)
开发语言·c#·wpf
bugcome_com2 天前
C# 字符串拼接全面指南
c#·.net·wpf
bugcome_com3 天前
WPF样式进阶实战:外置样式+MVVM主题切换+样式优先级全解析
c#·.net·wpf
lalala_Zou3 天前
场景题:电商平台订单未支付过期如何实现自动关闭订单?
wpf
czhc11400756633 天前
wpf 16
wpf
cn_mengbei4 天前
鸿蒙PC原生应用开发实战:ArkTS与DevEco Studio从零构建跨端桌面应用全栈指南
华为·wpf·harmonyos