WPF:从WPF Diagram Designer Part 4学习分组、对齐、排序、序列化和常用功能

在前面三篇文章中我们介绍了如何给图形设计器增加移动、选择、改变大小面板、缩略图、框线选择和工具箱连接等功能,本篇是这个图形设计器系列的最后一篇,将和大家一起来学习一下如何给图形设计器增加分组、对齐、排序、序列化等功能。

WPF Diagram Designer - Part 4

分组:Group, Ungroup

由于WPF不允许一个对象作为多个其他元素的子对象存在,而当移动父对象时,模板也会Unload导致一些问题,所以在这个系列中对分组的实现方式是:当分组一组元素时,内部生成一个Group,这个Group内部其实也是一个DesignerItem对象,只是IsGroup=true而已,在分组时,内部的对象的ParentID都置为这个Group对象的Id

 
public interface IGroupable
{
Guid ID {
get; }
Guid ParentID {
get; set; }
bool IsGroup { get; set; }
}

执行分组时的代码如下:

 

 
private void Group_Executed(object sender, ExecutedRoutedEventArgs e)
{
var items
= from item in this.SelectionService.CurrentSelection.OfType<DesignerItem>()
where item.ParentID == Guid.Empty
select item;

Rect rect
= GetBoundingRectangle(items);

DesignerItem groupItem
= new DesignerItem();
groupItem.IsGroup
= true;
groupItem.Width
= rect.Width;
groupItem.Height
= rect.Height;
Canvas.SetLeft(groupItem, rect.Left);
Canvas.SetTop(groupItem, rect.Top);
Canvas groupCanvas
= new Canvas();
groupItem.Content
= groupCanvas;
Canvas.SetZIndex(groupItem,
this.Children.Count);
this.Children.Add(groupItem);

foreach (DesignerItem item in items)
item.ParentID
= groupItem.ID;

this.SelectionService.SelectItem(groupItem);
}

当我们选择一个分组子对象时,设计器会选择这个分组以及分组的所有子对象

 

 

 
internal void SelectItem(ISelectable item)
{
this.ClearSelection();
this.AddToSelection(item);
}

internal void AddToSelection(ISelectable item)
{
if (item is IGroupable)
{
List
<IGroupable> groupItems = GetGroupMembers(item as IGroupable);

foreach (ISelectable groupItem in groupItems)
{
groupItem.IsSelected
= true;
CurrentSelection.Add(groupItem);
}
}
else
{
item.IsSelected
= true;
CurrentSelection.Add(item);
}
}

 

 

对齐:Align (Left, Right, Top, Bottom, Centered horizontal, Centered vertical)、Distribute (horizontal, vertical)

 

 
private void AlignLeft_Executed(object sender, ExecutedRoutedEventArgs e)
{
var selectedItems
= from item in SelectionService.CurrentSelection.OfType<DesignerItem>()
where item.ParentID == Guid.Empty
select item;

if (selectedItems.Count() > 1)
{
double left = Canvas.GetLeft(selectedItems.First());

foreach (DesignerItem item in selectedItems)
{
double delta = left - Canvas.GetLeft(item);
foreach (DesignerItem di in SelectionService.GetGroupMembers(item))
{
Canvas.SetLeft(di, Canvas.GetLeft(di)
+ delta);
}
}
}
}

 

 
private void AlignHorizontalCenters_Executed(object sender, ExecutedRoutedEventArgs e)
{
var selectedItems
= from item in SelectionService.CurrentSelection.OfType<DesignerItem>()
where item.ParentID == Guid.Empty
select item;

if (selectedItems.Count() > 1)
{
double center = Canvas.GetLeft(selectedItems.First()) + selectedItems.First().Width / 2;

foreach (DesignerItem item in selectedItems)
{
double delta = center - (Canvas.GetLeft(item) + item.Width / 2);
foreach (DesignerItem di in SelectionService.GetGroupMembers(item))
{
Canvas.SetLeft(di, Canvas.GetLeft(di)
+ delta);
}
}
}
}

 

 

排序:Order (Bring forward, Bring to top, Send backward, Send to back)

 

 
private void BringForward_Executed(object sender, ExecutedRoutedEventArgs e)
{
List
<UIElement> ordered = (from item in SelectionService.CurrentSelection
orderby Canvas.GetZIndex(item
as UIElement) descending
select item
as UIElement).ToList();

int count = this.Children.Count;

for (int i = 0; i < ordered.Count; i++)
{
int currentIndex = Canvas.GetZIndex(ordered[i]);
int newIndex = Math.Min(count - 1 - i, currentIndex + 1);
if (currentIndex != newIndex)
{
Canvas.SetZIndex(ordered[i], newIndex);
IEnumerable
<UIElement> it = this.Children.OfType<UIElement>().Where(item => Canvas.GetZIndex(item) == newIndex);

foreach (UIElement elm in it)
{
if (elm != ordered[i])
{
Canvas.SetZIndex(elm, currentIndex);
break;
}
}
}
}
}

序列化:Open, Save

使用XML保存,代码如下:

 
XElement serializedItems = new XElement("DesignerItems",
from item
in designerItems
let contentXaml
= XamlWriter.Save(((DesignerItem)item).Content)
select
new XElement("DesignerItem",
new XElement("Left", Canvas.GetLeft(item)),
new XElement("Top", Canvas.GetTop(item)),
new XElement("Width", item.Width),
new XElement("Height", item.Height),
new XElement("ID", item.ID),
new XElement("zIndex", Canvas.GetZIndex(item)),
new XElement("IsGroup", item.IsGroup),
new XElement("ParentID", item.ParentID),
new XElement("Content", contentXaml)
)
);

读取的时候需要建立Connection

 

 
private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
{
       ... 
foreach (XElement connectionXML in connectionsXML)
{
Guid sourceID
= new Guid(connectionXML.Element("SourceID").Value);
Guid sinkID
= new Guid(connectionXML.Element("SinkID").Value);

String sourceConnectorName
= connectionXML.Element("SourceConnectorName").Value;
String sinkConnectorName
= connectionXML.Element("SinkConnectorName").Value;

Connector sourceConnector
= GetConnector(sourceID, sourceConnectorName);
Connector sinkConnector
= GetConnector(sinkID, sinkConnectorName);

Connection connection
= new Connection(sourceConnector, sinkConnector);
Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element(
"zIndex").Value));
this.Children.Add(connection);
}
}

常用功能:Cut, Copy, Paste, Delete,Print

 

 
private void Cut_Executed(object sender, ExecutedRoutedEventArgs e)
{
CopyCurrentSelection();
DeleteCurrentSelection();
}

 

 
private void Print_Executed(object sender, ExecutedRoutedEventArgs e)
{
SelectionService.ClearSelection();

PrintDialog printDialog
= new PrintDialog();

if (true == printDialog.ShowDialog())
{
printDialog.PrintVisual(
this, "WPF Diagram");
}
}

 







 本文转自 jingen_zhou 51CTO博客,原文链接:http://blog.51cto.com/zhoujg/517448,如需转载请自行联系原作者


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392890.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

win7如何看计算机用户名和密码怎么办,win7系统电脑查看共享文件夹时不显示用户名和密码输入窗口的解决方法...

win7系统使用久了&#xff0c;好多网友反馈说win7系统电脑查看共享文件夹时不显示用户名和密码输入窗口的问题&#xff0c;非常不方便。有什么办法可以永久解决win7系统电脑查看共享文件夹时不显示用户名和密码输入窗口的问题&#xff0c;面对win7系统电脑查看共享文件夹时不显…

ASP.NET Core跨域设置

项目中经常会遇到跨域问题&#xff0c;解决方法&#xff1a; 在appsettings.json 文件中添加json项 {"Logging": {"LogLevel": {"Default": "Warning"}},"AllowedHosts": "*","AppCores": "https…

微信客户端<->腾讯微信服务器<->开发者服务器

出自 http://blog.csdn.net/hanjingjava/article/details/41653113 首先&#xff0c;通过Token验证&#xff0c;将公众号接入开发者服务器&#xff0c;这样客户端发给公众号的信息会被转发给开发者服务器&#xff1b; 第二&#xff0c;组装微信特定消息格式&#xff0c;返回给用…

idea提高调试超时_如何提高您的调试技能

idea提高调试超时by Nick Karnik尼克卡尼克(Nick Karnik) 如何提高您的调试技能 (How to Improve Your Debugging Skills) All of us write code that breaks at some point. That is part of the development process. When you run into an error, you may feel that you do…

leetcode 834. 树中距离之和(dp)

给定一个无向、连通的树。树中有 N 个标记为 0...N-1 的节点以及 N-1 条边 。第 i 条边连接节点 edges[i][0] 和 edges[i][1] 。返回一个表示节点 i 与其他所有节点距离之和的列表 ans。示例 1:输入: N 6, edges [[0,1],[0,2],[2,3],[2,4],[2,5]] 输出: [8,12,6,10,10,10] 解…

CSS设计指南(读书笔记 - 背景)

本文转自william_xu 51CTO博客&#xff0c;原文链接&#xff1a;http://blog.51cto.com/williamx/1140006&#xff0c;如需转载请自行联系原作者

在计算机网络中 带宽是什么,在计算机网络中,“带宽”用____表示。

答案查看答案解析:【解析题】计算机的发展经历了4个时代&#xff0c;各个时代划分的原则是根据()。【解析题】计算机网络的最主要的功能是______。【解析题】冯.诺依曼提出的计算机工作原理为____。【解析题】计算机的通用性使其可以求解不同的算术和逻辑问题&#xff0c;这主要…

如何在iOS上运行React Native应用

by Soujanya PS通过Soujanya PS 如何在iOS上运行React Native应用 (How to run a React Native app on iOS) I recently started to develop a React-Native app on iOS. This was my first foray into native app development. I was surprised by the ease and level of abs…

导出excel 后 页面按钮失效(页面假死)

在 page_load 里加上如下代码&#xff1a;string beforeSubmitJS "\nvar exportRequested false; \n"; beforeSubmitJS "var beforeFormSubmitFunction theForm.onsubmit;\n"; beforeSubmitJS "theForm.onsubmit function(){ \n"; …

Mysql分组查询group by语句详解

(1) group by的含义:将查询结果按照1个或多个字段进行分组&#xff0c;字段值相同的为一组(2) group by可用于单个字段分组&#xff0c;也可用于多个字段分组 select * from employee; --------------------------------------------- | num | d_id | name | age | sex | homea…

leetcode 75. 颜色分类(双指针)

给定一个包含红色、白色和蓝色&#xff0c;一共 n 个元素的数组&#xff0c;原地对它们进行排序&#xff0c;使得相同颜色的元素相邻&#xff0c;并按照红色、白色、蓝色顺序排列。 此题中&#xff0c;我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。 注意: 不能使用代码…

火车头如何才能设置发布的时候,如果是有html代码就直接的转换掉,互联网上笑话抽取及排重---火车头采集器的使用和MD5算法的应用...

10011311341 吕涛、10011311356李红目的&#xff1a;通过熟悉使用火车头采集器&#xff0c;在网络上采取3万条笑话并进行排重&#xff0c;以此来熟悉web文本挖掘的一些知识。过程&#xff1a;本次学习&#xff0c;主要分成两个部分。第一部分是笑话文本的采集&#xff0c;第二部…

Tcp_wrapper

在Linux进程分为&#xff1a;独立进程和非独立进程非独立进程&#xff1a;是依赖于超级守护进程的进程&#xff0c; 且受Xinetd 管理&#xff0c;并在启动服务时 必须启动例子&#xff1a;#chkconfig –level 2345 telnetd on关与chkconfig 的命令&#xff1a;#chkconfig –lis…

angular 动画_如何在Angular 6中使用动画

angular 动画介绍 (Introduction) Animation is defined as the transition from an initial state to a final state. It is an integral part of any modern web application. Animation not only helps us create a great UI but it also makes the application interesting…

win10上面安装win7的虚拟机怎么相互ping通

最近干了一些很蛋疼的事&#xff0c;这些都是自己踩过的坑&#xff0c;记录下来方便自己以后查阅 首先我的目的就是为了在自己的PC机上面部署一个SVN服务器&#xff0c;然后安装一个客户端&#xff0c;自己写的软件就可以定期入库&#xff0c;做好自己的版本控制&#xff0c;但…

新东方面试知识点记录

3.spring mvc 怎么接受http post 方式提交过来的xml数据&#xff1f;servlet中怎么接受&#xff1f; RequestMapping(value"/jsonPrase", headers {"content-typeapplication/json","content-typeapplication/xml"}) ResponseBody …

win10用计算机名访问文件夹,win10系统提示你当前无权访问该文件夹的解决方法【图文教程】...

Win10系统下&#xff0c;我们在访问或更改某些系统文件夹时&#xff0c;有时会遇到系统提示“你当前无权访问该文件夹”的情况。那么&#xff0c;遇到这种情况的话&#xff0c;我们该怎么办呢&#xff1f;接下来&#xff0c;小编就向大家分享win10系统提示“你当前无权访问该文…

.Net Micro Framework研究—实现SideShow窗体界面

基于MF系统的Windows SideShow界面是非常炫的&#xff08;如下图&#xff09;。既然微软能用.Net Micro Framework实现这么棒的界面效果&#xff0c;我想我们也能做到。 &#xff08;SideShow模拟器界面和游戏程序中的右键菜单—注意菜单弹出后&#xff0c;其它的界面变暗了&am…

leetcode 344. 反转字符串

编写一个函数&#xff0c;其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间&#xff0c;你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。…

事件捕获(capture)和冒泡事件(Bubble)

PS&#xff1a;这里是我从别人的博客中学习事件捕获和冒泡是的总结&#xff0c;如果你也感兴趣的话&#xff0c;建议你点击链接查看原博客的内容&#xff0c;他们写的都是很经典&#xff01; 对“捕获”和“冒泡”这两个概念&#xff0c;我想我们对冒泡更熟悉一些&…