Silverlight SL 4:模板化控件的奇怪行为
我们有一些xaml:Silverlight SL 4:模板化控件的奇怪行为,silverlight,silverlight-4.0,Silverlight,Silverlight 4.0,我们有一些xaml: <Style TargetType="local:V_RelLine"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:V_RelLine"> <Grid x:Name="LayoutRoot">
<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>
在运行时,startArrow和endArrow结束于同一点(即使它们被设置为不同的位置),就像它们结束于0,0一样。事实上,对Canvas.GetLeft(startArrow)的后续调用显示它处于0,0。
发生了什么事?为什么同一模板中的不同对象被分配了相同的坐标,最终位于不同的位置
感谢您对这方面的深入了解……这只是一个想法,但
画布。左
和画布。顶部
通常只有当元素放置在画布中而不是像您当前使用的网格中时才能正常工作。我开始怀疑这就是问题所在。。。为什么路径按预期显示,而省略号不按预期显示?Path
中图形的StartPoint
相对于包含的Path元素,因此它按预期显示。我不确定如何进一步简化答案,对于Canvas.Top
和Canvas.Left
属性,要处理省略号,省略号必须是Canvas
元素的子元素,而不是Grid
元素。
LinePathBezierFigure.StartPoint = startPoint;
Canvas.SetLeft(startArrow, startPoint.X);
Canvas.SetTop(startArrow, startPoint.Y);
/* similar for endArrow */