Wpf布局之Canvas面板!

文章目录


前言

Wpf布局之Canvas面板!

一、引言

Canvas面板允许使用精确的坐标来布置元素!

二、使用步骤

使用Canvas面板需要设置Canvas.Left和Canvas.Top附加属性,其中Canvas.Left属性是距离面板Canvas左边的单位数,Canvas.Top属性是距离面板Canvas顶部的单位数。另外也可以使用Canvas.Right属性来确定元素与Canvas面板的右边的距离,也可以使用Canvas.Bottom来确定元素与Canvas面板的底部的距离。但是不能同时使用Canvas.Left属性和Canvas.Right属性,也不能同时使用Canvas.Top属性和Canvas.Bottom属性。

可以使用Width和Height属性来确定元素的尺寸。如果没有设置这两个属性,则元素大小是根据内容大小来确定。

c 复制代码
<Grid>
    <Canvas>
        <Button Canvas.Left="10" Canvas.Top="20" Content="(10,20)"/>
        <Button Canvas.Left="50" Canvas.Bottom="50" Content="(50,50)"/>
        <Button Canvas.Left="60" Canvas.Top="80" Width="50" Height="50" Content="(60,80)"/>
        <Button Canvas.Left="70" Canvas.Top="120" Width="100" Height="50" Content="(70,120)"/>
    </Canvas>
</Grid>

效果图

如果Canva面板有多个元素重叠,则可以使用Z顺序,来控制它的重叠方式。可以通过设置Canvas.ZIndex附加属性的值来提高层次级别。通常元素具有相同的Canvas.ZIndex值-0,Canvas.ZIndex的值越大,层级就越高。设置Panel.ZIndex值也有相同的效果。

c 复制代码
<Grid>
    <Canvas>
        <Button Canvas.Left="10" Canvas.Top="20" Content="(10,20)"/>
        <Button Canvas.Left="50" Canvas.Bottom="50" Content="(50,50)"/>
        <Button Canvas.Left="60" Canvas.ZIndex="1"  Canvas.Top="80" Width="50" Height="50" Content="(60,80)"/>
        <Button Canvas.Left="70" Canvas.Top="120" Width="100" Height="50" Content="(70,120)"/>
    </Canvas>
</Grid>

效果图

相关推荐
张人玉9 小时前
C# WPF 折线图制作(可以连接数据库)
数据库·c#·wpf·sugar
闲人编程10 小时前
OpenTelemetry分布式追踪
分布式·wpf·trace·追踪·open telemetry·codecapsule
张人玉13 小时前
C# WPF 折线图制作笔记(LiveCharts 库)
笔记·c#·wpf·折线图·linechart
FuckPatience1 天前
WPF Matrix结构体方法ScaleAt的坐标系
wpf
我是小妖怪,潇洒又自在1 天前
springcloud alibaba(十)分布式事务
分布式·spring cloud·wpf
Poetinthedusk2 天前
设计模式-命令模式
windows·设计模式·c#·wpf·命令模式
棉晗榜2 天前
WPF印章水印, Border怎么悬浮在其他控件上面,类似盖章一样
wpf
张人玉2 天前
LiveCharts WPF MVVM 图表开发笔记
大数据·分布式·wpf·livecharts
武藤一雄2 天前
一款基于WPF开发的BEJSON转换工具
windows·c#·json·wpf
Poetinthedusk3 天前
设计模式-模板方法模式
windows·设计模式·c#·wpf·模板方法模式