首先知道Canvas有Left、Right、Top和Bottom这四个属性,放入Canvas的元素通过这四个属性来决定它们在Canvas里面的位置。

比如:

Xaml:

<Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
<Button Content="Button" Canvas.Left="72" Canvas.Top="45" Width="75" Height="38"/>
<Button Content="Button" Canvas.Left="124" Canvas.Top="173" Width="75" Height="42"/>
<Button Content="Button" Canvas.Left="387" Canvas.Top="64" Width="75" Height="46"/>
<Button Content="Button" Canvas.Left="336" Canvas.Top="196" Width="75" Height="42"/>
</Canvas>

如图:

Canvas中Z-Index属性:

不加Z-Index属性时,Canvas里面的元素会按照后定义的优先级更高,优先级高的可以覆盖优先级低的。

例:Xaml:

<Grid>
<Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
<Rectangle Fill="#FF5D5DC8" Height="45" Canvas.Left="22" Stroke="Black" Canvas.Top="25" Width="100"/>
<Rectangle Fill="#FFEAEAF6" Height="50" Canvas.Left="94" Stroke="Black" Canvas.Top="46" Width="100"/>
<Rectangle Fill="#FFCD6650" Height="100" Canvas.Left="166" Stroke="Black" Canvas.Top="84" Width="59"/>
<Ellipse Fill="#FFB1CD50" Height="100" Canvas.Left="195" Stroke="Black" Canvas.Top="143" Width="100"/>
</Canvas>
</Grid>

这里后定义的会覆盖先定义的子元素:

下面看看使用了Z-Index属性:

Xaml:

<Grid>
<Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
<Rectangle Fill="#FF5D5DC8" Panel.ZIndex="3" Height="45" Canvas.Left="22" Stroke="Black" Canvas.Top="25" Width="100"/>
<Rectangle Fill="#FFEAEAF6" Panel.ZIndex="2" Height="50" Canvas.Left="94" Stroke="Black" Canvas.Top="46" Width="100"/>
<Rectangle Fill="#FFCD6650" Panel.ZIndex="1" Height="100" Canvas.Left="166" Stroke="Black" Canvas.Top="84" Width="59"/>
<Ellipse Fill="#FFB1CD50" Panel.ZIndex="0" Height="100" Canvas.Left="195" Stroke="Black" Canvas.Top="143" Width="100"/>
</Canvas>
</Grid>

如图:

可以看出,Z-Index越小优先级越高,那么如果定义的Z-Index一样会是谁优先级更高呢?接着看。

Xaml:

<Grid>
<Canvas HorizontalAlignment="Left" Height="299" Margin="133,52,0,0" VerticalAlignment="Top" Width="526">
<Rectangle Fill="#FF5D5DC8" Panel.ZIndex="3" Height="45" Canvas.Left="22" Stroke="Black" Canvas.Top="25" Width="100"/>
<Rectangle Fill="#FFEAEAF6" Panel.ZIndex="3" Height="50" Canvas.Left="94" Stroke="Black" Canvas.Top="46" Width="100"/>
<Rectangle Fill="#FFCD6650" Panel.ZIndex="3" Height="100" Canvas.Left="166" Stroke="Black" Canvas.Top="84" Width="59"/>
<Ellipse Fill="#FFB1CD50" Panel.ZIndex="3" Height="100" Canvas.Left="195" Stroke="Black" Canvas.Top="143" Width="100"/>
</Canvas>
</Grid>

如图:

可以看出,Z-Index一样的时候,谁后定义谁的优先级就高。

WPF中Canvas使用的更多相关文章

  1. WPF 中Canvas图形移动、缩放代码

    从Flash转C#,很多内容一知半解,边摸索边前进,代码粗糙,权当留个脚印. 只是想得到一个基础的移动和缩放功能的界面,找了很久都是画线.画矩形等基础形状的代码,移动和缩放说的并不清晰,只能自己努力来 ...

  2. WPF在Canvas中绘图实现折线统计图

    最近在WPF中做一个需要实现统计的功能,其中需要用到统计图,之前也没有接触过,度娘上大多都是各种收费或者免费的第三方控件,不想用第三方控件那就自己画一个吧. 在园子还找到一篇文章,思路来自这篇文章,文 ...

  3. 在WPF中的Canvas上实现控件的拖动、缩放

    如题,项目中需要实现使用鼠标拖动.缩放一个矩形框,WPF中没有现成的,那就自己造一个轮子:) 造轮子前先看看Windows自带的画图工具中是怎样做的,如下图: 在被拖动的矩形框四周有9个小框,可以从不 ...

  4. WPF中通过代码设置控件的坐标

    用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...

  5. WPF中图形表示语法详解(Path之Data属性语法)ZZ

    大可山 [MSN:a3news(AT)hotmail.com] http://www.zpxp.com 萝卜鼠在线图形图像处理 ------------------------------------ ...

  6. WPF技巧-Canvas转为位图

    转自:http://www.cnblogs.com/tmywu/archive/2010/09/14/1825650.html 在WPF中我们可以将Canvas当成一种画布,将Canvas中的控件当成 ...

  7. WPF中model属性即时改变

    新建一个model作为说明即可,以便查阅. 添加引用:using System.ComponentModel ; public class Test:INotifyPropertyChanged { ...

  8. WPF中如何使用图像API进行绘制

    首先,由于WPF中不象GDI+中有Graphics对象,因此你无法使用Graphics进行绘图了,取而代之的是:DrawingContext:类似地,GDI+中的OnPaint已被OnRender取代 ...

  9. WPF中的资源简介、DynamicResource与StaticResource的区别(转)

    什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎可以包含图像.字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性. 也就是 ...

随机推荐

  1. springMVC拦截配置

    1.web.xml文件配置 <!-- spring mvc --> <servlet> <servlet-name>DispatcherServlet</se ...

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">详解

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. git 的安装使用以及协作流程

    git安装: sudo apt-get install git-core git使用: 转:https://www.liaoxuefeng.com/wiki/0013739516305929606dd ...

  4. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  5. LeetCode: 620 Not Boring Movies(easy)

    题目: X city opened a new cinema, many people would like to go to this cinema. The cinema also gives o ...

  6. 网络编程-TCP连接-readLine

    Server: package com.net.tcp; import java.io.BufferedReader; import java.io.IOException; import java. ...

  7. CF438E The Child and Binary Tree(生成函数+多项式开根+多项式求逆)

    传送门 可以……这很多项式开根模板……而且也完全不知道大佬们怎么把这题的式子推出来的…… 首先,这题需要多项式开根和多项式求逆.多项式求逆看这里->这里,这里讲一讲多项式开根 多项式开方:已知多 ...

  8. pytest入门学习(2)

    pytest的hello world pyt1.py def func(x): print (x+1); return x+1; def test_answer(): assert func(3) = ...

  9. C#字体字号的改变

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  10. springboot2.x 的 RedisCacheManager变化

    springboot2.x 的 RedisCacheManager变化 springboot2.x 的 RedisCacheManager变化 由于最近在学着使用redis做缓存,使用的是spring ...