2011-09-07 91 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对象)的可视化树。