惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
Jina AI
Jina AI
雷峰网
雷峰网
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
美团技术团队
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
博客园 - Franky
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
量子位
IT之家
IT之家
人人都是产品经理
人人都是产品经理
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - seabluescn

WPF开发快速入门【8】WPF进行简单的3D开发 WPF开发快速入门【7】WPF的拖放功能(Drag and Drop) WPF开发快速入门【6】下拉框与枚举类型 WPF开发快速入门【5】DataGrid的使用 WPF开发快速入门【4】自定义控件与用户控件 WPF开发快速入门【3】WPF的基本特性(附加属性) WPF开发快速入门【2】WPF的基本特性(Style、Trigger、Template) WPF开发快速入门【0】前言与目录 WPF优秀组件推荐之FreeSpire WPF优秀组件推荐之LiveCharts WPF优秀组件推荐之MahApps WPF优秀组件推荐之Stylet(二) WPF优秀组件推荐之Stylet(一) TensorFlow.NET机器学习入门【9】后记 TensorFlow.NET机器学习入门【8】采用GPU进行学习 TensorFlow.NET机器学习入门【7】采用卷积神经网络(CNN)处理Fashion-MNIST TensorFlow.NET机器学习入门【6】采用神经网络处理Fashion-MNIST TensorFlow.NET机器学习入门【5】采用神经网络实现手写数字识别(MNIST) TensorFlow.NET机器学习入门【4】采用神经网络处理分类问题
WPF开发快速入门【1】WPF的布局
seabluescn · 2022-08-23 · via 博客园 - seabluescn

概述

本文描述几款WPF中常用的布局控件。

Grid

Grid是WPF最常用的布局控件。 它把面板分割为固定长和宽的网格,子控件就放置在网格内。

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="hello" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="10"/>
    </Grid>

Grid控件有两个显著的特点:

1、行高和列宽可以设定为固定值,也可以按比例分配;

2、可以跨行或跨列。

StackPanel

StackPanel按顺序依次排列控件,通过Orientation="Horizontal"或Orientation="Vertical"来控制列表的方向。 

    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Hello"/>
    </StackPanel>

GridSplitter

通过GridSplitter可以调整两个网格的宽度或高度。 

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" MinWidth="150"/>
                    <ColumnDefinition Width="3"/>
                    <ColumnDefinition Width="2*" MinWidth="200"/>
                </Grid.ColumnDefinitions>

                <Grid Grid.Column="0" Background="WhiteSmoke"/>
                <GridSplitter Grid.Column="1" Background="CornflowerBlue"/>
                <Grid Grid.Column="2" Background="LightGray" />
            </Grid>

GridSplitter的宽度和颜色都可以设置。

DockPanel

DockPanel控件可以在主要的显示面板周五显示可以停靠的面板。 

            <DockPanel>
                <Grid Width="200" DockPanel.Dock="Left" Background="SeaGreen" Visibility="{Binding LeftVisibility}"/>
                <Grid Width="200" DockPanel.Dock="Right" Background="Orchid" Visibility="{Binding RightVisibility}"/>
                <Grid  Background="Silver">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>

                    <Button Grid.Column="0" Content="Left" Width="80" Height="30" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5"
                            Command="{s:Action LeftClick}"/>
                    <Button Grid.Column="2" Content="Right" Width="80" Height="30" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5"
                            Command="{s:Action RightClick}"/>
                </Grid>
            </DockPanel>

需要注意的是,主面板一定要放在最后描述。通过下面代码可以显示或隐藏Dock面板: 

    public class PageLayoutViewModel : Screen
    {
        public bool IsLeftShow { get; set; } = true;
        public bool IsRightShow { get; set; } = true;

        public Visibility LeftVisibility => IsLeftShow ? Visibility.Visible : Visibility.Collapsed;
        public Visibility RightVisibility => IsRightShow ? Visibility.Visible : Visibility.Collapsed;

        public void LeftClick()
        {
            IsLeftShow = !IsLeftShow;
        }

        public void RightClick()
        {
            IsRightShow = !IsRightShow;
        }
    }

View Code

DockPanelSplitter

有没有既可以停靠又可以调整宽度的面板呢?官方没有提供,但git上有。

下载地址:JVimes/DockPanelSplitter: Like WPF's GridSplitter, but for DockPanel instead of Grid. (github.com)

资源

系列目录:WPF开发快速入门【0】前言与目录 

代码下载:Learn WPF: WPF学习笔记 (gitee.com)