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>

效果图

相关推荐
脚踏实地皮皮晨32 分钟前
003003002_WPF Grid 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
脚踏实地皮皮晨1 天前
003002004_WPF Panel 基类 官方类定义
开发语言·windows·算法·c#·wpf·visual studio
湿滑路面1 天前
Rouyan:使用WPF/C#构建的基于LLM的快捷翻译小工具
开发语言·c#·wpf
软萌萌的12 天前
C#中的多级缓存架构设计与实现深度解析
缓存·c#·wpf
dalong102 天前
WPF:3D正八面体自动旋转
3d·wpf
仙宇觉尘2 天前
WPF Stylet可以如何实现导航功能?
java·开发语言·wpf
丙氨酸長鏈2 天前
WPF 引用 ASP.NET Core 的 AOT 版本
后端·asp.net·wpf
Xin_ye100862 天前
第六章:破局——WPF内存泄漏解决方案
c#·wpf
FuckPatience2 天前
WPF Button Content 的StringFormat
java·前端·wpf
dalong103 天前
WPF:3D正四面体旋转改错
3d·wpf