PathGeometry的路径标记语法

时间:2014-07-29 20:00:12

标签: silverlight xaml

使用path markup syntax,您可以使用钻石等自定义形状轻松创建Path

<Path Data="M 0,5 L 5,0 L 10,5 L 5,10 Z" />

是否可以在PathGeometry中使用此语法?根据上面的链接(适用于WPF而不是Silverlight),我尝试使用PathGeometry.Figures

<local:SomeControl>
    <local:SomeControl.Geometry>
        <PathGeometry Figures="M 0,5 L 5,0 L 10,5 L 5,10 Z" />
    </local:SomeControl.Geometry>
</local:SomeControl>

但这会引发XamlParseException

  

无法创建&#39; PathGeometry.Figures&#39;从文本&lt; M 0,5 L 5,0 L 10,5 L 5,10 Z&#39;

我真的需要使用下面的扩展表单,还是有某种方法可以使用短字符串?

<PathGeometry>
    <PathFigure StartPoint="0,5" IsClosed="True" IsFilled="True">
        <LineSegment Point="5,0" />
        <LineSegment Point="10,5" />
        <LineSegment Point="5,10" />
    </PathFigure> 
</PathGeometry>

1 个答案:

答案 0 :(得分:1)

我刚注意到Blend SDK有一个ConvertToPathGeometry辅助方法。因此,使用以下方法创建IValueConverter很容易:

return GeometryHelper.ConvertToPathGeometry((string)parameter);

然后可以将Geometry设置为PathGeometry实例,如下所示:

<local:SomeControl Geometry="{Binding ConverterParameter='M 0,5 L 5,0 L 10,5 L 5,10 Z',
                                      Converter={StaticResource PathGeometryConverter}}"
                                  />