本帖最后由 yuhuimoon 于 2015-12-17 13:43 编辑
该问题已经解决。
下面是部分代码,供大家参考:
需要先定义xaml描述文件,用于定义shape的样式,具体方法可以参考官方文档。其中,ShapePoint就是我的自定义锚点
[XML] 纯文本查看 复制代码 <p:ResourceDictionary xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:DevExpress.Diagram.Core.Shapes;assembly=DevExpress.Diagram.v15.2.Core">
<ShapeTemplate x:Key="{ShapeKey CustomShape1}" DefaultSize="100,100" Columns="*;P0;*" Rows="*;P0;*">
<Start X="1" Y="0" />
<Line X="2" Y="0" />
<Line X="2" Y="1" />
<Line X="3" Y="1" />
<Line X="3" Y="2" />
<Line X="2" Y="2" />
<Line X="2" Y="3" />
<Line X="1" Y="3" />
<Line X="1" Y="2" />
<Line X="0" Y="2" />
<Line X="0" Y="1" />
<Line X="1" Y="1" />
<ShapeTemplate.ConnectionPoints>
<ShapePoint X="1.5" Y="0" />
<ShapePoint X="3" Y="1.5" />
<ShapePoint X="1.5" Y="3" />
<ShapePoint X="0" Y="1.5" />
<ShapePoint X="0" Y="0" />
<ShapePoint X="3" Y="3" />
</ShapeTemplate.ConnectionPoints>
<ShapeTemplate.Parameters>
<Parameter DefaultValue="20"
Point="CreatePoint((P + W) / 2, H)"
Value="2 * P.X - W"
Min="100" Max="Min(W, H)" />
</ShapeTemplate.Parameters>
</ShapeTemplate>
</p:ResourceDictionary>
然后定义自定义shape的构建类:
[C#] 纯文本查看 复制代码 public static class CustomShapeBuilder
{
static System.Windows.FrameworkElement element = null;
public static string getCustomShapeName(string id)
{
return DevExpress.Diagram.Core.DiagramToolboxRegistrator.GetStencil(id).Name;
}
public static DiagramStencil GetCustomStencil(string id)
{
return DevExpress.Diagram.Core.DiagramToolboxRegistrator.GetStencil(id);
}
public static void RegisterCustomShapes()
{
if (element == null)
{
element = new System.Windows.FrameworkElement();
}
var dict = new System.Windows.ResourceDictionary();
dict.Source = new Uri("E:\\DiagramResources.xaml", UriKind.RelativeOrAbsolute);
DevExpress.Diagram.Core.DiagramToolboxRegistrator.RegisterShapes("CustomShapes", () => "Custom Shapes", dict, getCustomShapeName);
}
}
最后在需要创建的地方调用,有点需要说明,我的程序没有使用DiagramToolBox工具箱,只是获取了DiagramShape对象:
[C#] 纯文本查看 复制代码 CustomShapeBuilder.RegisterCustomShapes();
DiagramStencil myStencil = CustomShapeBuilder.GetCustomStencil("CustomShapes");
ShapeDescription myDescription = myStencil.GetShape("CustomShape1");
DiagramShape _shape = new DiagramShape(myDescription, 0, 0, 100, 100, theShape.Content);
运行程序,创建出了一个Shape,一共有六个锚点(上、下、左、右、左上、右下):
|