2010-09-08 88 views
4

只是想知道如何去沿着路徑繪製箭頭。路徑將改變方向並經歷幾個不同的點。箭頭設計用於向用戶顯示他們需要沿着路徑行進的方向。WPF繪製箭頭沿路徑

我試圖用刷子刷,但因爲我需要的箭頭頭它們導向異體沿着路徑沒有工作......

回答

5

Path Animations OverviewMatrixAnimationUsingPath

它可以用於沿着PathGeometry移動控件,並且如果您設置了DoesRotateWithTangent,控件也將沿着路徑的方向旋轉。

EDIT1:

<Page.Resources> 
    <PathGeometry x:Key="Path" x:Shared="False" Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100"/> 
</Page.Resources> 
<Canvas Width="400" Height="400"> 
    <Path Data="{StaticResource Path}" Stroke="Blue" StrokeThickness="1"/> 
    <Path 
     x:Name="Arrow1" 
     Stretch="Fill" 
     Width="16" Height="16" StrokeLineJoin="Miter" 
     Data="M 0 -5 L 10 -5 M 5 0 L 10 -5 L 5 -10" 
     Stroke="Black" StrokeThickness="3"> 
     <Path.RenderTransform> 
      <TransformGroup> 
       <TranslateTransform X="-8" Y="-8"/> 
       <MatrixTransform> 
        <MatrixTransform.Matrix> 
         <Matrix/> 
        </MatrixTransform.Matrix> 
       </MatrixTransform> 
      </TransformGroup> 
     </Path.RenderTransform> 
     <Path.Triggers> 
      <EventTrigger RoutedEvent="Path.Loaded"> 
       <BeginStoryboard> 
        <Storyboard> 
         <MatrixAnimationUsingPath 
          Storyboard.TargetName="Arrow1" 
          Storyboard.TargetProperty="RenderTransform.Children[1].Matrix"         
          DoesRotateWithTangent="True" 
          Duration="0:0:5" 
          BeginTime="0:0:0" 
          RepeatBehavior="Forever" PathGeometry="{StaticResource Path}" >        
         </MatrixAnimationUsingPath> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Path.Triggers> 
    </Path> 
</Canvas> 

EDIT2:計算有多少箭需要

我假設你正在創建一個自定義的控制和編程方式添加箭頭? 如果是這樣,我認爲最簡單的方法是爲單個循環和BeginTimeGap指定持續時間,後續箭頭的BeginTime之間的時間。你必須添加箭頭數量將持續時間/ BeginTimeGap,或在簡化代碼:

while (BeginTime < Duration) 
{ 
    //add arrow with BeginTime and Duration; 
    BeginTime += BeginTimeGap; 
} 

獲取箭頭之間的正確的速度和間隔將回落到調整這兩個值。

+0

我已經看了一下,我不認爲這是我所追求的內容。 – 2010-09-08 15:24:03

+0

你確定?嘗試運行我添加的xaml ...如果這不是你想要的,可以嘗試澄清你的問題? – Bubblewrap 2010-09-08 15:55:57

+0

好吧,我很感興趣;)好吧,如何修改這個圖標,讓一個箭頭沿着40px的間隔移動......所以它就像一個連續的箭頭一樣的流? – 2010-09-08 16:11:30