เรามี xaml บางส่วน:
<Style TargetType="local:V_RelLine">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:V_RelLine">
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
</VisualStateManager.VisualStateGroups>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Ellipse x:Name="startArrow" Height="20" Width="60" Fill="Green" Stroke="Blue" Visibility="Visible" />
<Path x:Name="LinePathPart" Visibility="Visible" Stroke="Red" StrokeDashArray="2 2" StrokeThickness="2"
>
<Path.Data>
<PathGeometry x:Name="LinePathGeometry" >
<PathFigure x:Name="linePathBezierFigure" >
<BezierSegment x:Name="linePathBezierSegment" />
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
<Ellipse x:Name="endArrow" Height="20" Width="20" Fill="Red" Stroke="Red" Visibility="Visible" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
และในโค้ดด้านหลัง:
LinePathBezierFigure.StartPoint = startPoint;
Canvas.SetLeft(startArrow, startPoint.X);
Canvas.SetTop(startArrow, startPoint.Y);
/* similar for endArrow */
ขณะรันไทม์ startArrow และ endArrow จะจบลงที่จุดเดียวกัน (แม้ว่าจะตั้งค่าไว้ที่ตำแหน่งต่างกันก็ตาม) ราวกับว่าจบลงที่ 0,0
อันที่จริง การเรียก Canvas.GetLeft(startArrow) ครั้งต่อไปจะแสดงขึ้น ว่ามันอยู่ที่ 0,0
เกิดอะไรขึ้น? เหตุใดวัตถุที่แตกต่างกันในเทมเพลตเดียวกัน กำหนดพิกัดเดียวกัน และจบลงที่ตำแหน่งต่างกัน
ขอบคุณสำหรับความเข้าใจในเรื่องนี้ ....