开发者

Access StoryBoard from a template

Here is some class I got :

<Style x:Key="BoutonMenu" TargetType="{x:Type Button}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Margin" Value="5,5,5,5"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="BtnHover" AutoReverse="False" RepeatBehavior="Forever">
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="contentPresenter">
                            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="-1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="BtnClick" AutoReverse="False">
                        <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="border">
                            <EasingPointKeyFrame KeyTime="0:0:0.1" Value="0.5,0.5"/>
                            <EasingPointKeyFrame KeyTime="0:0:0.15" Value="0.5,0.5"/>
                            <EasingPointKeyFrame KeyTime="0:0:0.25" Value="0.5,0.5"/>
                            <EasingPointKeyFrame KeyTime="0:0:0.4" Value="0.5,0.5"/>
                            <EasingPointKeyFrame KeyTime="0:0:0.6" Value="0.5,0.5"/>
                            <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
                        </PointAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="border">
                            <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="-1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="-1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="-1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
                            <EasingColorKeyFrame KeyTime="0:0:1" Value="#FF337641"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="BtnRevert">
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(Sol开发者_StackOverflow中文版idColorBrush.Color)" Storyboard.TargetName="rectangle">
                            <EasingColorKeyFrame KeyTime="0" Value="#FF337641"/>
                            <EasingColorKeyFrame KeyTime="0:0:0.5" Value="Black"/>
                        </ColorAnimationUsingKeyFrames>
                        <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="border">
                            <EasingPointKeyFrame KeyTime="0:0:0.5" Value="0.5,0.5"/>
                        </PointAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="border">
                            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </ControlTemplate.Resources>
                <Border x:Name="border" BorderBrush="#FF000000" Background="#FF000000" BorderThickness="0" CornerRadius="20" Opacity="1" >
                    <Border.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </Border.RenderTransform>
                    <Grid x:Name="grid">
                        <Border x:Name="mask" Background="White" CornerRadius="20"/>
                        <!-- Background Layer -->
                        <Grid x:Name="grid1">
                            <Grid.OpacityMask>
                                <VisualBrush Visual="{Binding ElementName=mask}"/>
                            </Grid.OpacityMask>
                            <Rectangle x:Name="rectangle" Fill="#FF000000" >
                                <Rectangle.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                            <Rectangle x:Name="RefractionLayer">
                                <Rectangle.Fill>
                                    <RadialGradientBrush GradientOrigin="0.496,1.052">
                                        <RadialGradientBrush.RelativeTransform>
                                            <TransformGroup>
                                                <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/>
                                                <TranslateTransform X="0.02" Y="0.3"/>
                                            </TransformGroup>
                                        </RadialGradientBrush.RelativeTransform>
                                        <GradientStop Offset="0.819" Color="#00000000"/>
                                        <GradientStop Color="#FF020000" Offset="0.187"/>
                                    </RadialGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>

                            <ContentPresenter x:Name="contentPresenter" Margin="0,2,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
                                <ContentPresenter.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </ContentPresenter.RenderTransform>
                            </ContentPresenter>

                        </Grid>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="ButtonBase.Click">
                        <StopStoryboard BeginStoryboardName="BtnHover_BeginStoryboard"/>
                        <StopStoryboard BeginStoryboardName="BtnClick_BeginStoryboard"/>
                        <BeginStoryboard x:Name="BtnClick_BeginStoryboard" Storyboard="{StaticResource BtnClick}"/>
                    </EventTrigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.ExitActions>
                            <RemoveStoryboard BeginStoryboardName="BtnHover_BeginStoryboard"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard x:Name="BtnHover_BeginStoryboard" Storyboard="{StaticResource BtnHover}"/>
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True"/>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" Value="0.25"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

As you can see, I have 3 different storyboards, two of which happen on specific triggers. I have a third one (BtnRevert) that I would like to start on demand. I can't really seem to find how to do it. Is this possible, if yes how and if not how can I bypass that ?

Edit : So I tried this :

        Dim s As Storyboard = _oCurMenuBouton.Template.Resources("BtnRevert")
        s.Begin()

It seems to get the storyboard right, but now I get this :

No applicable name scope exists to resolve the name 'rectangle'.

It seems it does not recognize the storyboard has being the property of my button or at least the template. Is there any way to make it work ?

Got it to work ! Here is the answer

        Dim s As Storyboard = _oCurMenuBouton.Template.Resources("BtnRevert")
        s.Begin(_oCurMenuBouton,_oCurMenuBouton,Template)

Thanks


just try this

  <Button Height="25"
            Margin="5"
            Style="{StaticResource BoutonMenu}" Click="Button_Click"></Button>

in the code Behind

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        var rButton = sender as Button;
        if (rButton != null)
        {
            Storyboard s = (Storyboard)rButton.Template.Resources["BtnRevert"];
        }  
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜