2012-03-05 100 views
0

我發現的答案:How to rotate an image to a particular angle in Windows Phone 7 Silverlight Application?與我正在尋找的接近。在Windows Phone上旋轉樣式圖像

我的問題是 - 如果圖像是樣式的一部分,我該怎麼做?圖像基本上是指向運動方向(軌跡)的箭頭。

<Style x:Key="MyBoatPushPinStyle" TargetType="maps:Pushpin"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Image x:Name="MyBoatIcon" 
         Source="Resources/Icons/myboat.png" 
         Stretch="None"> 
        <Image.RenderTransform> 
         <RotateTransform Angle="0" /> 
        </Image.RenderTransform> 
       </Image> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

樣式應用於MapLayer:

<maps:MapLayer x:Name="LocationLayer"> 
    <maps:Pushpin Style="{StaticResource MyBoatPushPinStyle}" 
    Location="{Binding CurrentLocation}" /> 
</maps:MapLayer> 

我想不通的是如何風格中引用的圖像,如果可以真正實現。

喜歡的東西:

((RotateTransform)REFERENCE_TO_IMAGE.RenderTransform).Angle = _currentTrack; 

回答

0

試試這個:

<maps:MapLayer x:Name="LocationLayer"> 
    <maps:Pushpin x:Name="PushpinLayer" Style="{StaticResource MyBoatPushPinStyle}" Location="{Binding CurrentLocation}" /> 
</maps:MapLayer> 

Image a = FindFirstElementInVisualTree<Image>(PushpinLayer); 
if (a != null) 
    ((RotateTransform)a.RenderTransform).Angle = 90; 

private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject 
{ 
    var count = VisualTreeHelper.GetChildrenCount(parentElement); 
    if (count == 0) 
     return null; 

    for (int i = 0; i < count; i++) 
    { 
     var child = VisualTreeHelper.GetChild(parentElement, i); 

     if (child != null && child is T) 
      return (T)child; 

     else 
     { 
      var result = FindFirstElementInVisualTree<T>(child); 
      if (result != null) 
       return result; 

     } 
    } 
     return null; 
} 
+0

感謝MyKuLLSKI。正是我需要的:) – Declan 2012-03-08 13:10:43