【问题标题】:Traversing A Control Template in Silverlight在 Silverlight 中遍历控件模板
【发布时间】:2011-09-07 06:29:51
【问题描述】:

我有一个这样的控件模板

    <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 并调用其中之一。

请帮忙。提前致谢。

【问题讨论】:

    标签: c# .net silverlight controltemplate


    【解决方案1】:

    听起来您应该根据您的示例 xaml 更改视觉状态。

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

    或者这是您只有使用 ControlTemplate 对控件的引用

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

    【讨论】:

    • 其实,如果我说得更具体的话,我需要故事板。 Silverlight中没有办法遍历控件模板吗?
    • 但是为什么需要情节提要?在我看来,您想从一种 UI 状态播放一些动画到另一种状态。为此,您应该使用 VisualStates。
    【解决方案2】:

    您不能在控件模板中命名元素,因为没有生成匹配的设计器代码隐藏。元素的命名通过运行时搜索可视树中的名称,并在用户控件中的 InitializeObject 调用期间为它们分配成员对象。

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

    但是,您可以使用 VisualTreeHelper 迭代可视化树以查找特定元素类型(在您的情况下为 Storyboard 对象)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多