向xaml动态添加线段
本文关键字:添加 动态 xaml | 更新日期: 2023-09-27 18:06:32
我有一个在画布上可视化包含多个线段的路径的问题。
现在用户可以用直接路径连接画布上的两个控件。这是通过创建一个视图模型类的实例来实现的,该实例被传递给一个模板选择器,该模板选择器返回路径的数据模板。
这个模板看起来像
<Path StrokeThickness="2"
Stroke="Black"
Fill="Black"
MinWidth="1"
MinHeight="1"
Name="arrowPath">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure IsClosed="False"
StartPoint="{Binding Path=Source, ElementName=_this}">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment IsStroked="True"
Point="{Binding Path=Destination, ElementName=_this}" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
<PathFigure IsClosed="True"
IsFilled="True"
StartPoint="{Binding Path=Destination, ElementName=_this}">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment IsSmoothJoin="True"
IsStroked="True"
Point="{Binding Path=TrianglePoint2, ElementName=_this}" />
<LineSegment IsSmoothJoin="True"
IsStroked="True"
Point="{Binding Path=TrianglePoint3, ElementName=_this}" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
路径的起始和端点属性绑定到视图模型实例的属性。
到目前为止一切正常。但是有了更多的控件,画布真的很乱,我想让用户有机会通过由多个线段组成的路径连接控件。新的视图模型类存储在列表中单击鼠标时的鼠标位置。
设计新的控件,我不知道如何动态地在我的xaml中为我的列表中的每个点添加一个linesegement。
我希望你明白我的意思,谢谢。
考虑一下这篇CodeProject文章,它包含了创建关系图设计器的分步教程,包括正交连接器路由以及如何在UI中对它们建模。