如何在XAML(Silverlight)中的资源字典中绑定`Path.Data`

时间:2015-04-20 05:17:21

标签: c# xaml silverlight binding

似乎无法在 Silverlight 中的ResourceDictionary中定义几何体。所以我使用Path来存储一些几何。这是 Path.xaml 文件:

<ResourceDictionary ///some namespaces///>
    <Path x:Key="path1" Data="//some geometry//" />
    <Path x:Key="path2" Data="//some geometry//" />
    <Path x:Key="path3" Data="//some geometry//" />
</ResourceDictionary>

引用此ResourceDictionary后,我想在UserControl中的某处使用这些几何:

<Path Data = "{Binding Source={StaticResouce path1}", Path=Data}"/>

但我收到以下错误:

  

价值不在预期范围内。

如何在XAML中获取这些几何图形?

1 个答案:

答案 0 :(得分:4)

纠正你不能。我遇到了这个问题并通过将路径字符串作为类的属性然后将该类添加为资源来解决它。例如,这是类

public class IconPaths
{
    public string CircleIcon
    {
        get { return "M50.5,4.7500001C25.232973,4.75 4.75,25.232973 4.7500001,50.5 4.75,75.767029 25.232973,96.25 50.5,96.25 75.767029,96.25 96.25,75.767029 96.25,50.5 96.25,25.232973 75.767029,4.75 50.5,4.7500001z M50.5,0C78.390381,0 101,22.609621 101,50.5 101,78.390381 78.390381,101 50.5,101 22.609621,101 0,78.390381 0,50.5 0,22.609621 22.609621,0 50.5,0z"; }
    }

    public string LogIcon 
    {
        get { return "M16.6033,19.539C18.922133,19.539 20.042,21.777275 20.042,23.919949 20.042,26.367331 18.793436,28.397999 16.5877,28.397999 14.394864,28.397999 13.149,26.334829 13.149,24.032551 13.149,21.665472 14.301367,19.539 16.6033,19.539z M5.3724453,18.578932L5.3724453,29.357607 11.370038,29.357607 11.370038,28.189699 6.7645523,28.189699 6.7645523,18.578932z M28.522547,18.46693C24.908003,18.46693 22.700978,20.817846 22.685377,24.032669 22.685377,25.711081 23.260882,27.151291 24.189095,28.045797 25.244007,29.053604 26.587723,29.469608 28.217943,29.469608 29.67366,29.469608 30.905476,29.101805 31.529183,28.877802L31.529183,23.696165 27.97814,23.696165 27.97814,24.816576 30.169766,24.816576 30.169766,28.029497C29.848162,28.189699 29.225756,28.317898 28.314345,28.317898 25.803812,28.317898 24.156695,26.703287 24.156695,23.968267 24.156695,21.265749 25.867714,19.634937 28.490048,19.634937 29.57736,19.634937 30.297468,19.84334 30.872776,20.097841L31.20888,18.963034C30.745175,18.739132,29.770062,18.46693,28.522547,18.46693z M16.666903,18.40313C13.788068,18.40313 11.661542,20.641445 11.661542,24.064671 11.661542,27.326992 13.660466,29.534107 16.506901,29.534107 19.258234,29.534107 21.510861,27.567295 21.510861,23.856268 21.510861,20.657745 19.609839,18.40313 16.666903,18.40313z M14.433776,2.6588883C12.967757,2.6588886,11.773743,3.8522573,11.773743,5.319067L11.773743,15.361408 34.872822,15.361408C35.828533,15.361408,36.603447,16.136215,36.603447,17.092621L36.603447,30.392214C36.603447,31.347221,35.828533,32.121925,34.872822,32.121925L11.773743,32.121925 11.773743,37.347263C11.773743,38.814075,12.967757,40.007481,14.433776,40.007481L35.931637,40.007481C37.397755,40.007481,38.59177,38.814075,38.59177,37.347263L38.59177,12.690889 29.754463,12.690889C29.022553,12.690889,28.424946,12.092585,28.424946,11.36008L28.424946,2.6601186 28.424946,2.6588883z M14.433776,0L29.723061,0 41.251999,11.530681 41.251999,37.347263C41.251999,40.280281,38.866474,42.667,35.931637,42.667L14.433776,42.667C11.498941,42.667,9.1135704,40.280281,9.1135704,37.347263L9.1135704,32.121925 1.7293315,32.121925C0.7749116,32.121925,1.9777951E-07,31.347221,0,30.392214L0,17.092621C1.9777951E-07,16.136215,0.7749116,15.361408,1.7293315,15.361408L9.1135704,15.361408 9.1135704,5.319067C9.1135704,2.3854568,11.498941,0,14.433776,0z"; }
    }
}

这是在资源字典中调用的

<LocalStyles:IconPaths x:Key="IconPaths"/>

最终用于路径

<Path Data="{Binding Source={StaticResource IconPaths}, Path=ArrowIcon}" Height="10"  Stretch="Fill"  Fill="DimGray" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5">