C# 在Silverlight中遍历控件模板

C# 在Silverlight中遍历控件模板,c#,.net,silverlight,controltemplate,C#,.net,Silverlight,Controltemplate,我有一个这样的控件模板 <ControlTemplate TargetType="Button"> <Grid > <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualStateGroup.Transitions> <!--Take one

我有一个这样的控件模板

    <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>

**
**
我必须遍历此控件模板以获取名为
selectedbuttonnstoryboard
的故事板,或者获取视觉状态
SelectedButton
,并调用其中一个


请帮忙。提前感谢。

听起来您更应该根据示例xaml更改visualstate

VisualStateManager.GoToState(this, "SelectedButton", true);
或者这是使用ControlTemplate对控件的引用

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

无法在控件模板中命名元素,因为生成的设计器代码后面没有匹配的设计器代码。元素命名的工作原理是在用户控件中的InitializeObject调用期间,运行时搜索可视树中的名称,并为其分配成员对象

模板中的元素仅在运行时有效地添加到可视化树中


但是,您可以使用迭代视觉树来查找特定的元素类型(在您的例子中是序列图像板对象)。

实际上,如果我说得更具体,我需要序列图像板。在silverlight中没有方法遍历控件模板吗?但是为什么需要脚本呢?听起来你想从一个UI状态到另一个UI状态播放一些动画。为此,您应该使用VisualStates。