开发者

Traversing A Control Template in Silverlight

开发者 https://www.devze.com 2023-04-03 08:18 出处:网络
I\'ve a control template like this <ControlTemplate TargetType=\"Button\"> <Grid > <VisualStateManager.VisualStateGroups>

I've a control template like this

    <ControlTemplate TargetType="Button">
  <Grid >
    <VisualStateManager.VisualStateGroups>
      <VisualStateGrou开发者_高级运维p 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>

I've to traverse this control template to get the storyboard named SelectedButtonStoryboard or get the visual state SelectedButton and to invoke the either one.

Please help. Thanks in advance.


It sounds like you should rather change the visualstate based on your example xaml.

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

Or this is you only have a reference to the control using the ControlTemplate

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


You can't name elements in a control template as there is no matching designer code-behind generated. Naming of elements works by a runtime search for names in the visual tree, and assigning member objects to them, during the InitializeObject call in a user control.

The elements in a template are effectively added to the visual tree at runtime only.

You can however use VisualTreeHelper to iterate the visual tree looking for specific element types (in your case the Storyboard objects).

0

精彩评论

暂无评论...
验证码 换一张
取 消