我在我的XAML文件中定义的资源,如下所示:
<Path x:Key="myPath"
Data="M14.773241,18.080208 C12.373256,18.080208 10.239936,19.30687 10.239936,27.573483
L10.239936,36.106766 C10.239936,45.440037 12.586588,46.506699 14.986573,46.506699
C18.613216,46.506699 19.359879,42.400059 19.359879,35.3601 L19.359879,27.733482
C19.359879,20.05353 17.386559,18.080208 14.773241,18.080208 z M14.879907,11.786915
C17.973221,11.786915 22.293194,13.013573 24.906511,17.920212 C26.773167,21.386856
27.519829,27.093487 27.519829,32.213455 C27.519829,34.506775 27.306496,41.706726
24.906511,46.453365 C23.626518,49.013351 20.906536,52.799992 15.199905,52.799992
C2.1333201,52.799992 2.1333201,37.600086 2.1333201,32.160122 C2.1333201,28.05348
2.1333201,22.666847 4.4266391,18.453541 C5.8666301,15.840225 8.639946,11.786915
14.879907,11.786915 z"
/>
我希望能够多这条道路(和其他几个人)的“实例”添加到一个StackPanel。 当然,我不能简单地“mypath中”添加到面板上,因为它已经是另一个容器的孩子。
不过,我似乎无法能够要么克隆的路径。 我试过了:
Path clone = new Path()
{
Data = source.Data
};
但没有运气...约值的例外是把预期范围。
最后,我想挖成source.Data(一的PathGeometry),但不包含PathFigures ......我不知道为什么,因为如果我从资源部分直接复制到面板的路径中呈现。
是什么赋予了?
谢谢,塞尔吉奥
把路径数据在一个字符串资源:
<Page.Resources>
<system:String x:Key="PathData">
M14.773241,18.080208 C12.373256,18.080208 10.239936,19.30687 10.239936,27.573483
L10.239936,36.106766 C10.239936,45.440037 12.586588,46.506699 14.986573,46.506699
C18.613216,46.506699 19.359879,42.400059 19.359879,35.3601 L19.359879,27.733482
C19.359879,20.05353 17.386559,18.080208 14.773241,18.080208 z M14.879907,11.786915
C17.973221,11.786915 22.293194,13.013573 24.906511,17.920212 C26.773167,21.386856
27.519829,27.093487 27.519829,32.213455 C27.519829,34.506775 27.306496,41.706726
24.906511,46.453365 C23.626518,49.013351 20.906536,52.799992 15.199905,52.799992
C2.1333201,52.799992 2.1333201,37.600086 2.1333201,32.160122 C2.1333201,28.05348
2.1333201,22.666847 4.4266391,18.453541 C5.8666301,15.840225 8.639946,11.786915
14.879907,11.786915 z
</system:String>
</Page.Resources>
并使用它:
<Path x:Name="Path1" Data="{StaticResource PathData}" Fill="Blue" ... />
<Path x:Name="Path2" Data="{StaticResource PathData}" Fill="Red" ... />
你需要这在你的XAML声明:
xmlns:system="clr-namespace:System;assembly=mscorlib"
如果你想通过程序生成路径中使用一个共同的路径字符串,Silverlight是缺少一点是WPF具有重要功能 - 让你不得不杂牌吧:
string pathXaml =
@"<Path xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
Data=""path_data_goes_here"" />";
Path path = (Path)System.Windows.Markup.XamlReader.Load(pathXaml);