当需要动态添加、修改、删除控件时,如果要达到最好的效果,肯定不只是把需要的控件添加到容器中,并且还需要把容器中的已有控件进行排序操作(置顶、置底、前移、后移操作)。由于初次接触到wpf,所以对很多知识都不熟悉,现自己动手实现了一个Zindex的操作算法。

      /// <summary>
/// 操作控件的Z顺序
/// </summary>
/// <param name="sender">菜单</param>
/// <param name="moveToFront">True 前移 ,false 置后</param>
/// <param name="toBottom">true 移动至底端,false 移动一层</param>
private void doMoveZindex(object sender, bool moveToFront, bool toBottom)
{
UIElement ui = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
if (ui is FrameworkElement)
{
FrameworkElement fui = ui as FrameworkElement;
if (fui == null) return; int nowZIndex = Canvas.GetZIndex(fui); //deviceOperator.DeviceCollection保存的就是每个控件对应设备的位置和z顺序值
//取得最大z顺序
int maxZindex = deviceOperator.DeviceCollection
.Select(t => t.ZIndex1)
.Max();
//需要转换的zindex
int nextZindex = -;
if (moveToFront) //前移
{
//如果已经位于最顶层,则取消
if (nowZIndex == maxZindex) return;
if (toBottom) //移动到最顶层
{
nextZindex = maxZindex;
foreach (FrameworkElement childElement in parentCanvas.Children)
{
int zi = Canvas.GetZIndex(childElement); if (zi > nowZIndex)
{
Canvas.SetZIndex(childElement, zi - );
updateDevice(childElement);
maxZindex = -;
}
}
}
else//上移一层
{
nextZindex = nowZIndex + ;
foreach (FrameworkElement childElement in parentCanvas.Children)
{
if (Canvas.GetZIndex(childElement) == nextZindex)
{
Canvas.SetZIndex(childElement, nowZIndex);
updateDevice(childElement);
maxZindex = -;
break;
}
} }
if (maxZindex == -)
{
Canvas.SetZIndex(fui, nextZindex); updateDevice(fui);
}
}
else //置底
{
if (nowZIndex == ) return;
if (toBottom)
{
nextZindex = ;
foreach (FrameworkElement childElement in parentCanvas.Children)
{
int zi = Canvas.GetZIndex(childElement);
if (zi < nowZIndex)
{
Canvas.SetZIndex(childElement, zi + );
updateDevice(childElement);//保存控件的Z顺序值到deviceoperator.DeviceCollection
maxZindex = -;
}
}
}
else
{
nextZindex = nowZIndex - ;
foreach (FrameworkElement childElement in parentCanvas.Children)
{
if (Canvas.GetZIndex(childElement) == nextZindex)
{
Canvas.SetZIndex(childElement, nowZIndex);
updateDevice(childElement);
maxZindex = -;
break;
}
}
}
if (maxZindex == -)
{
Canvas.SetZIndex(fui, nextZindex);
updateDevice(fui);
}
}
}
}
//调用方法
//前移
doMoveZindex(sender, true, false);
//置项
doMoveZindex(sender, true, true);
//后移
doMoveZindex(sender, false, false);
//置底
doMoveZindex(sender, false, true);

这样的一个前提条件是容器中的每一个控件的ZIndex值都不会相同。

待解决的问题:现在变化的z顺序,是针对整个容器来说的。如果可以针对控件的重叠区域,进行修改Z顺序,那么速度应该会得到相应的提升。

WPF 容器的Z顺序操作的更多相关文章

  1. Fiddler 插件开发,使用 WPF 作为 UI 控件

    Fiddler 插件的 UI,本身使用的 WinForm,这个例子是使用 WinForm 中的 WPF 容器,将 WPF 控件作为 Fiddler 插件的 UI 使用. 为什么使用 WPF ?为了自适 ...

  2. WPF布局原则

    WPF系统使用基于流布局的布局标准,开发人员创建与显示分辨率和窗口大小无关的用户界面.在不同显示器上可以进行很好的缩放. 首先来谈一谈布局原则: WPF窗口只能包含一个元素(Window元素属于内容控 ...

  3. 使用DotNetBar制作漂亮的WinFrom界面,自定义AgileEAS.NET SOA平台WinClient主界面

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  4. 【转】PV3D的小练习~太阳系八大行星

    转自:http://hi.baidu.com/boycy/item/70d1ba53bc8c3a958c12eddf http://www.cnblogs.com/flash3d/archive/20 ...

  5. Vulkan Tutorial 28 Depth buffering

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 到目前为止,我们所使用的几何图形为3D,但仍然完全扁平的. ...

  6. L2-020. 功夫传人*

    L2-020. 功夫传人 参考博客 #include<vector> #include<cstring> #include<algorithm> using nam ...

  7. LeetCode——Binary Search Tree Iterator

    Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...

  8. Javascript笔记部分

    写入HTML输出 document.write(“<h1>”); 改变HTML内容 x = document.getElementById(“demo”) //查找元素 后面可以.valu ...

  9. 【Python】使用torrentParser1.03对多文件torrent的分析结果

    Your environment has been set up for using Node.js 8.5.0 (x64) and npm. C:\Users\horn1>cd C:\User ...

随机推荐

  1. maven加载jar包配置

    maven build时报程序包不存在和找不到符号的错误,但是代码中不报错,如下: [ERROR] Failed to execute goal org.apache.maven.plugins:ma ...

  2. 逻辑操作符“&&”的三层理解

    第一层:操作符“&&”可以对两个布尔值进行逻辑与运算,返回一个布尔值. 第二层:操作符“&&”可以对两个真假值进行逻辑与运算,并且返回一个真假值. 第三层:操作符“&a ...

  3. Examples of complexity pattern

    O(1):constant - the operation doesn't depend on the size of its input, e.g. adding a node to the tai ...

  4. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. Could not load java.net.BindException错误解决

    出现了错误异常:信息: Illegal access: this web application instance has been stopped already.  Could not load ...

  6. Linux运维入门到高级全套常用要点

    Linux运维入门到高级全套常用要点 目 录 1. Linux 入门篇................................................................. ...

  7. Python更换国内源实现快速PIP安装

    WINDOWS 安装pip 1.首先下载安装Python,并将python的安装目录添加进系统环境变量 2.复制这个文件保存为.py并执行 https://bootstrap.pypa.io/get- ...

  8. 反人类的java

  9. gzip压缩及测试方法【转载】

    Nginx开启Gzip压缩大幅提高页面加载速度 http://www.veryhuo.com/a/view/51706.html 刚刚给博客加了一个500px相册插件,lightbox引入了很多js文 ...

  10. windows下使用ffmpeg进行视频转换和截图。

    author:fanfq(xiaoban) Email:fangqing.fan#gmail.comlink:http://fanfq.iteye.com/admin/blogs/655569chan ...