Telerik提供了RadDiagram控件,用于图形元素的旋转,拖拽和缩放.更重要的是,它还拓展了许多绑定的命令(复制,剪切,粘贴,回退等等).

我们可以用来组织自己的图形编辑工具.

Step1.定义图形元素容器(Shape)的基类,继承RadDiagramShape,并重写Serialize和Deserialize方法,来定制将来对象的保存或复制.

    /// <summary>
/// 图形Shape控件
/// </summary>
public class FigureShape : RadDiagramShape
{
public FigureShape()
{
IsConnectorsManipulationEnabled = false;
}
/// <summary>
/// 序列化
/// </summary>
/// <returns></returns>
public override SerializationInfo Serialize()
{
SerializationInfo serializationInfo = base.Serialize(); try
{
var obj = base.Content as FigureBase;
if (obj != null)
{
IFormatter formatter = new BinaryFormatter();
using (var ms = new MemoryStream())
{
formatter.Serialize(ms, obj);
serializationInfo["Figure"] = Convert.ToBase64String(ms.ToArray());
}
}
}
catch (Exception e)
{
throw new Exception("序列化过程失败:" + e.Message);
}
return serializationInfo;
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="serializationInfo"></param>
public override void Deserialize(SerializationInfo serializationInfo)
{
base.Deserialize(serializationInfo); try
{
if (serializationInfo["Figure"] != null)
{
var buffer = Convert.FromBase64String(serializationInfo["Figure"].ToString());
IFormatter formatter = new BinaryFormatter();
using (var ms = new MemoryStream(buffer))
{
Content = formatter.Deserialize(ms);
//绑定Shape坐标和Figure坐标
this.DataContext = Content;
var binding = new Binding("Position") { Mode = BindingMode.TwoWay };
this.SetBinding(PositionProperty, binding);
}
}
}
catch (Exception e)
{
throw new Exception("反序列化过程失败:" + e.Message);
}
}
}

Step2.定义图形元素基类,并支持可序列化

    /// <summary>
/// 图形基类
/// </summary>
[Serializable]
public abstract class FigureBase : NotificationObject
{
/// <summary>
/// 图形位置
/// </summary>
private Point position;
public Point Position
{
get { return position; }
set { position = value; RaisePropertyChanged("Position"); }
}
}

Step3.定义基本图形元素,继承FigureBase,只列出一个示例,不再详述

    [Serializable]
public class StationFig : FigureBase
{
/// <summary>
/// xml节点构造
/// </summary>
/// <param name="node"></param>
public StationFig(XmlNode node)
{
var infoNode = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(s => s.Name == "use");
var xAttri = infoNode.GetAttributeByName("x");
var yAttri = infoNode.GetAttributeByName("y"); this.Position = new Point(double.Parse(xAttri), double.Parse(yAttri));
this.StationType = infoNode.GetAttributeByName("class");
} /// <summary>
/// 厂站类型(220kv,500kv)
/// </summary>
private string stationType;
public string StationType
{
get { return stationType; }
set { stationType = value; RaisePropertyChanged("StationType"); }
}
}

Step4.定义图形元素的样式

    <!--线路样式-->
<DataTemplate DataType="{x:Type svgFigs:LineFig}" >
<Path x:Name="path" StrokeThickness="" Data="{Binding Path}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding LineType}" Value="kv500">
<Setter Property="Stroke" Value="Yellow" TargetName="path"/>
</DataTrigger>
<DataTrigger Binding="{Binding LineType}" Value="kv220">
<Setter Property="Stroke" Value="White" TargetName="path"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<!--厂站样式-->
<DataTemplate DataType="{x:Type svgFigs:StationFig}" >
<Ellipse x:Name="ellipse" Width="" Height="" Fill="Transparent" StrokeThickness=""/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding StationType}" Value="kv500">
<Setter Property="Stroke" Value="Yellow" TargetName="ellipse"/>
</DataTrigger>
<DataTrigger Binding="{Binding StationType}" Value="kv220">
<Setter Property="Stroke" Value="White" TargetName="ellipse"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<!--文本样式-->
<DataTemplate DataType="{x:Type svgFigs:TextFig}" >
<TextBlock x:Name="text" Foreground="White" FontFamily="{Binding FontFamily}" FontSize="{Binding FontSize}" Text="{Binding Text}"/>
</DataTemplate>

Step5.界面编辑工具面板

<telerik:RadDiagram x:Name="diagram"  MinWidth="" BorderThickness=""
Background="Black"
IsBackgroundSurfaceVisible="False"
IsSnapToItemsEnabled="False"
IsSnapToGridEnabled="False"/>

Step6.关键步骤,定义Shape容器中ContentTemplate显示内容

<Style TargetType="{x:Type common:FigureShape}">
<Setter Property="BorderBrush" Value="{x:Null}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Position" Value="{Binding Position,Mode=TwoWay}"/>
<Setter Property="Content" Value="{Binding}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type common:FigureShape}">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Step7.增加图形元素到面板

        /// <summary>
/// 增加图元到绘图面板
/// </summary>
/// <param name="figure"></param>
private void AddFigureToDiagram(FigureBase figure)
{
var shape = new FigureShape() { DataContext = figure };
diagram.AddShape(shape);
}

编辑工具示例:(支持图元的旋转,移动,缩放,复制粘贴等操作,属性编辑,缩略图导航...)

【Telerik控件学习】-建立自己的图形编辑工具(Diagram)的更多相关文章

  1. 【Telerik控件学习】-制作3D效果的柱状图(ChartView)

    首先,定义柱状图,并设置自定义的DataTemplate <telerik:RadCartesianChart > <telerik:RadCartesianChart.Horizo ...

  2. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  3. DevExpress控件学习总结(转)

    DevExpress控件学习总结   1.Navigation & Layout 1.1 Bar Manager 如果想在窗体或用户控件(user control)上添加工具条(bars)或弹 ...

  4. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

  5. WinFrom 第三方控件 TeleRik控件

    1.首先从工具-拓展与应用中下载安装  TeleRik WinFroms VsExtensions   TeleRik dll文件     2.工具箱控件  将Telerik控件更新过来 3.新建一个 ...

  6. wxPython控件学习之wx.grid.Grid 表格控件

    wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...

  7. 转)delphi chrome cef3 控件学习笔记 (二)

    (转)delphi chrome cef3 控件学习笔记 (二) https://blog.csdn.net/risesoft2012/article/details/51260832 原创 2016 ...

  8. PyQt5Day03--程序基本结构之面向对象版本+控件学习

    1.程序基本结构之面向对象版本 (1)开发阶段(自己写好并测试)——设置为模版qto from PyQt5.Qt import * class Window(QWidget): def __init_ ...

  9. DevExpress控件学习总结 z

    1.Navigation & Layout 1.1 Bar Manager 如果想在窗体或用户控件(user control)上添加工具条(bars)或弹出菜单(popup menus),我们 ...

随机推荐

  1. Spring4 customEditors

    Spring4.0版本以后customEditors属性为Map<Class<?>, Class<? extends PropertyEditor>>,所以用key ...

  2. 随便讲讲我对于svn和git的想法

    1.SVN是集中式版本管理工具,而Git是分布式版本管理工具,这是核心区别. 二者都有集中的库,只是git偏向于分布式,用户可以再自己电脑上克隆一份自己的库,即使在断网的情况下也能够查看版本,创建分支 ...

  3. 利用angularJs自定义指令(directive)实现在页面某一部分内滑块随着滚动条上下滑动

    最近老大让我一个效果实现在页面某一部分内滑块随着滚动条上下滑动,说明一下我们项目使用技术angularJs.大家都知道,使用jquery很好实现. 那么angular如何实现呢,我用的是自定义指令(d ...

  4. 如何查看maven plugin所包含的goal

    maven项目的构建生命周期(build lifecycle)由很多阶段组成:从validate到deploy. maven插件中包含goal.这些goal可以被绑定到不同的maven的构建阶段上.g ...

  5. CDS测试框架介绍:如何为ABAP CDS Entities写测试

    动机 现在大家都知道单元测试对我们代码的好处.并且我们都承认它是开发过程中不可或缺的一部分.但是在把代码切换到数据库的模式下的时候,我们被粗暴地打回了软件测试的黑暗年代...我们现在面临着逻辑下推到A ...

  6. Python初识2

    27.Python中没有{}括起来的代码块,使用pass来代替c语言中的{(无内容)}: 28.使用__name__来指示模块是如何加载的,如果是被导入的,那么__name__就是该模块的名字,如果是 ...

  7. Apache CXF入门

    CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了.CXF 继承了 Celtix 和 XFire 两大 ...

  8. 使用phpmailer插件发邮件失败提示:SMTP -> ERROR: Failed to connect to server: Connection timed out (110) smtp connect() failed;

    一个邮件发送问题,整整弄了我一周时间,起因是这样的,之前弄的一个网站,需要在邮箱里面认证之后才可以注册成功.网站上线了差不多一年之后,客户突然跟我说,网站不能注册了,然后我就查看了一下代码. 发现报这 ...

  9. append、extend与insert的区别

    最近在自学Python语言,看到向列表增加更多数据时被append(),extend(),insert()方法绕晕了. 作为编程0基础的小白,觉得有必要自己再梳理一遍: 1.append()方法是指在 ...

  10. Net知识图谱

    对于Web系统开发来说,Net其实也是有好多知识点需要学的,虽然目前JAVA是主流,就业市场比较大,但Net也在积极的拥抱开源,大Net Core 2 出来了,这无疑给Net开发者带来更大的希望,好了 ...