2011-09-07 27 views
0

我有這樣的遍歷控制模板在Silverlight

<ControlTemplate TargetType="Button"> 
    <Grid > 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="CommonStates"> 

     <VisualStateGroup.Transitions> 

      <!--Take one half second to trasition to the MouseOver state.--> 
      <VisualTransition To="MouseOver" 
           GeneratedDuration="0:0:0.5"/> 
     </VisualStateGroup.Transitions> 

     <VisualState x:Name="Normal" /> 

     <!--Change the SolidColorBrush, ButtonBrush, to red when the 
      mouse is over the button.--> 
     <VisualState x:Name="MouseOver"> 
      <Storyboard> 
      <ColorAnimation Storyboard.TargetName="ButtonBrush" 
          Storyboard.TargetProperty="Color" To="Red" /> 
      </Storyboard> 
     </VisualState> 
     **<VisualState x:Name="SelectedButton"> 
      <Storyboard x:Name="SelectedButtonStoryboard"> 
      <ColorAnimation Storyboard.TargetName="ButtonBrush" 
          Storyboard.TargetProperty="Color" To="Red" /> 
      </Storyboard> 
     </VisualState>** 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 
    <Grid.Background> 
     <SolidColorBrush x:Name="ButtonBrush" Color="Green"/> 
    </Grid.Background> 
    </Grid> 
</ControlTemplate> 

我已經遍歷該控件模板獲得命名SelectedButtonStoryboard故事板或獲得的可視狀態SelectedButton和調用任何一個控件模板。

請幫忙。提前致謝。

回答

1

這聽起來像你應該改變基於你的示例xaml的visualstate。

VisualStateManager.GoToState(this, "SelectedButton", true); 

或者這是你只需要使用的ControlTemplate

VisualStateManager.GoToState(controlInstance, "SelectedButton", true); 
+0

其實,如果我更具體地說,我需要故事板。有沒有辦法在Silverlight中遍歷控件模板? –

+0

但你爲什麼需要故事板?這聽起來對你而言,你想從一種UI狀態播放一些動畫到另一種。爲此,你應該使用VisualStates。 – Oliver

1

因爲沒有匹配的設計師後臺代碼生成你不能說出在控制模板元素的控制提供參考。 元素的命名通過運行時搜索可視化樹中的名稱並在用戶控件的InitializeObject調用期間爲其分配成員對象。

模板中的元素僅在運行時纔會有效地添加到可視化樹中。

但是,您可以使用VisualTreeHelper來迭代查找特定元素類型(在您的情況下爲Storyboard對象)的可視化樹。