WPF学习(5)- Border控件(边框布局)+GridSplitter分割窗口

严格来说,Border并不是一个布局控件,因为它并不是Panel的子类,而是Decorator装饰器的子类,而Decorator继承于FrameworkElement。我们要先看看它的父类Decorator。

csharp 复制代码
public class Decorator : FrameworkElement, IAddChild
{
    public Decorator();
 
    public virtual UIElement Child { get; set; }
    protected override int VisualChildrenCount { get; }
    protected internal override IEnumerator LogicalChildren { get; }
 
    protected override Size ArrangeOverride(Size arrangeSize);
    protected override Visual GetVisualChild(int index);
    protected override Size MeasureOverride(Size constraint);
 
}

Decorator 装饰器只有一个Child 属性,说明Decorator只能容纳一个子元素(UIElement),也就是Border只能容纳一个子元素。

再看看Border的结构定义:

csharp 复制代码
public class Border : Decorator
{
    public static readonly DependencyProperty BorderThicknessProperty;
    public static readonly DependencyProperty PaddingProperty;
    public static readonly DependencyProperty CornerRadiusProperty;
    public static readonly DependencyProperty BorderBrushProperty;
    public static readonly DependencyProperty BackgroundProperty;
 
    public Border();
 
    public Thickness BorderThickness { get; set; }
    public Thickness Padding { get; set; }
    public CornerRadius CornerRadius { get; set; }
    public Brush BorderBrush { get; set; }
    public Brush Background { get; set; }
 
    protected override Size ArrangeOverride(Size finalSize);
    protected override Size MeasureOverride(Size constraint);
    protected override void OnRender(DrawingContext dc);
 
}
  • BorderThickness:设置Border边框的厚度(像素宽度)。
  • Padding:设置子元素相对于Border边框的距离。
  • CornerRadius:设置Border的圆角。
  • BorderBrush:设置Border边框的颜色画刷。
  • Background:设置Border的背景颜色画刷。

正是因为Border有这么多实用的属性, 所以, 我们通常在布局界面时,Border(装饰器)控件是首选。

xml 复制代码
    <WrapPanel Margin="10">
        <Border Height="35" Margin="10" Padding="5" BorderThickness="1" BorderBrush="Gray">
            <TextBlock  Text="矩形 - Border控件" Margin="5" />
        </Border>
        <Border Height="35" Margin="10" Padding="5" BorderThickness="1" BorderBrush="Gray" CornerRadius="20">
            <TextBlock  Text="椭圆 - Border控件" Margin="5" />
        </Border>
        <Border Width="150" Height="150" Margin="10" Padding="5" BorderThickness="1" 
                Background="Red" BorderBrush="Gray" CornerRadius="75">
            <TextBlock  Text="圆形Border控件" Margin="5" HorizontalAlignment="Center" 
                        FontSize="16" FontWeight="Bold" VerticalAlignment="Center" Foreground="White"/>
        </Border>
    </WrapPanel>

我们分别写了3个Border,第一个Border被设计成矩形,第二个Border增加了圆角属性,第三个Border通过CornerRadius属性,将值设置为宽度或高度的一半,就形成了一个正圆。

GridSplitter分割窗口

GridSplitter控件用来分割窗体的布局,必须放在Grid栅格控件中配合使用,通过鼠标按住GridSplitter进行左右或上下拖动,即可调整行列尺寸。

注意:

  1. 如果你希望GridSplitter控件可以水平调整左右的Grid列宽时,那么HorizontalAlignment属性必须设置为Stretch或者Center。
  2. 如果你希望GridSplitter控件可以垂直调整行高,那么VerticalAlignment属性必须设置为Stretch或者Center。
  3. ShowsPreview属性表示拖动时是否及时绘制调整尺寸。
xml 复制代码
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0" Background="LightBlue">
            <TextBlock TextWrapping="Wrap" Padding="10" LineHeight="20">
            1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
            </TextBlock>
        </Border>
        <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Center"   ShowsPreview="False"/>
        <Border Grid.Column="2" Background="LightCoral">
            <TextBlock TextWrapping="Wrap" Padding="10" LineHeight="20">
                222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
            </TextBlock>
        </Border>
    </Grid>

最好是为GridSplitter单独分配一行或者一列,同时,GridSplitter需要跨越整行或整列,这样的效果会更好。

如上面的代码所示,我们在Grid中分割了3个单元格(3列),将GridSplitter居在放置,简单设置一下GridSplitter的属性,就可以达到我们的目的了。

相关推荐
茯苓gao10 小时前
嵌入式开发笔记:CAN与CAN FD完全指南——从帧格式差异到MCU选型实战
网络·笔记·stm32·嵌入式硬件·学习·汽车
木木_王10 小时前
嵌入式学习 | STM32 裸板驱动开发(Day06) | DHT11温湿度传感器超详解(单总线时序+全套驱动代码+智能温湿度报警项目)
驱动开发·stm32·学习
FellAveal11 小时前
【Go语言入门学习笔记】Part19.速率控制、原子计数器、互斥锁(Mutex)、状态协程
笔记·学习·golang
你怎么知道我是队长11 小时前
经典蓝牙(Bluetooth Classic / BR/EDR)学习计划
学习
极地野狼 音乐哔哔11 小时前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
学习·microsoft
oier_Asad.Chen13 小时前
【洛谷题解/AcWing题解】洛谷P4011 孤岛营救问题/AcWing1131拯救大兵瑞恩
数据结构·笔记·学习·算法·动态规划·图论·宽度优先
xxwl58513 小时前
Git 完整学习笔记:从入门到团队协作
笔记·git·学习
minglie114 小时前
zynq的xlnx_rebase_v5.4_2020.2分支打patch-5.4.3-rt1.patch.xz补丁
学习
醉城夜风~14 小时前
HTML常用标签详解学习博客:从基础标签到页面结构完整掌握
前端·学习·html
minglie115 小时前
蚂蚁S9矿板PS 内核定时器驱动
学习