1
奇怪的行爲,我們有一些XAML:SL 4:用模板控制
<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。
這是怎麼回事?爲什麼同一個模板中的不同對象分配了相同的座標,最終在不同的位置?
感謝在這個任何見解....
我開始懷疑這是問題... 爲什麼路徑顯示如預期,而橢圓不? – Number8 2010-08-02 21:29:41
「Path」中圖形的'StartPoint'是相對於包含Path元素的,因此它按預期顯示。我不太清楚我如何能夠進一步簡化答案,Canvas.Top和Canvas.Left屬性在橢圓上工作時,橢圓必須是Canvas元素的子元素而不是Grid元素。 – AnthonyWJones 2010-08-03 08:15:10