Search code examples
vb.netxamlanimationstoryboardcode-behind

Storyboard not available in codebehind


Why can I not access to the storyboard when using WPF. In Silverlight, the exact same code works.

Codebehind VB

Public Class UserControl1

    Private Sub UserControl1_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
        Me.storyboardBlend.Begin()
    End Sub
End Class

XAML:

 <UserControl x:Class="UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

        <Image x:Name="imgOn" Source="/OUTPUT%20-%20WPF01;component/Images/Disk.png" />
        <Grid.Resources>
            <Storyboard x:Key="storyboardBlend" x:Name="storyboardBlend">
                <DoubleAnimation x:Name="AnimationOpacityOn" Storyboard.TargetName="imgOn" Storyboard.TargetProperty="Opacity" Duration="0:0:1.500" To="1" />
            </Storyboard>

        </Grid.Resources>
    </Grid>
</UserControl>

Solution

  • To use a string as key works now for the storyboard. But I have still no access to the animation parameter which is the next step.

    XAML part:

                <Grid.Resources>
                <Storyboard x:Key="storyboardBlend" x:Name="storyboardBlend">
                    <DoubleAnimation x:Name="AnimationOpacityOn" Storyboard.TargetName="imgOn" Storyboard.TargetProperty="Opacity" Duration="0:0:1.500" To="1" />
                    <DoubleAnimation x:Name="AnimationOpacityOff" Storyboard.TargetName="imgOff" Storyboard.TargetProperty="Opacity" Duration="0:0:1.500" To="1" />
                    <DoubleAnimation x:Name="AnimationOpacitOnGlow" Storyboard.TargetName="imgOnGlow" Storyboard.TargetProperty="Opacity" Duration="0:0:1.500" To="1" />
                </Storyboard>
    
            </Grid.Resources>
    

    Hack in Codebehind which is not working:

    Private _Checked As Boolean
    Public Property Checked As Boolean
        Get
            Return _Checked
        End Get
        Set(ByVal value As Boolean)
            _Checked = value
    
            Dim storyboard As Storyboard = LayoutTextImage.Resources("storyboardBlend")
            storyboard.Stop()
            If _Checked = True Then
    
                Dim Anim As DoubleAnimation
                Anim = storyboard("AnimationOpacityOn")
                Anim.To = 1
    
                Anim = Me.FindResource("AnimationOpacityOff")
                Anim.To = 0
    
                Anim = Me.FindResource("AnimationOpacityOnGlow")
                Anim.To = 1
    
    
                'Dim storyboard As Storyboard = Me.FindResource("storyboardBlend")
                storyboard.Begin()
    
            Else
                Dim Anim As DoubleAnimation
                Anim = Me.FindResource("AnimationOpacityOn")
                Anim.To = 0
    
                Anim = Me.FindResource("AnimationOpacityOff")
                Anim.To = 1
    
                Anim = Me.FindResource("AnimationOpacityOnGlow")
                Anim.To = 0
    
                storyboard.Begin()
    
            End If
        End Set
    End Property