Edward Tanguay
Edward Tanguay

Reputation: 193312

Why does accessing my Storyboard x:Name work in Silverlight but not in WPF?

The following WPF code gets the error: The name 'zipButtonOut' does not exist in the current context.

However, the identical code works in Silverlight as I demonstrate here: http://tanguay.info/web/index.php?pg=codeExamples&id=65

What do I have to do to the WPF code to be able to access the Storyboard within Window.Resources? I tried it in a WPF UserControl as well but got the same error.

XAML:

<Window x:Class="TestDataGrid566.Test1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test1" Height="300" Width="300">
    <Window.Resources>
        <Storyboard x:Name="zipButtonOut" x:Key="zipButtonOut">
            <DoubleAnimation Storyboard.TargetName="buttonContinue"
Storyboard.TargetProperty="Width"
From="0" To="300" Duration="0:0:.2"></DoubleAnimation>
            <DoubleAnimation Storyboard.TargetName="buttonContinue"
Storyboard.TargetProperty="Height"
From="2" To="50" Duration="0:0:.4"></DoubleAnimation>
        </Storyboard>
    </Window.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel HorizontalAlignment="Center" Margin="20">
            <Button x:Name="buttonBegin" Content="Click here to begin" Background="Green" Click="buttonBegin_Click"/>
            <Button x:Name="buttonContinue" Margin="0 70 0 0" Width="160" Height="2" FontSize="18" Background="Yellow"
Content="Click here to continue" Visibility="Collapsed"></Button>
        </StackPanel>
    </Grid>
</Window>

code-behind:

using System.Windows;

namespace TestDataGrid566
{
    public partial class Test1 : Window
    {
        public Test1()
        {
            InitializeComponent();
        }

        private void buttonBegin_Click(object sender, RoutedEventArgs e)
        {
            buttonBegin.Visibility = Visibility.Collapsed;
            buttonContinue.Visibility = Visibility.Visible;
            //zipButtonOut.Begin(); //GETS ERROR: The name 'zipButtonOut' does not exist in the current context.
        }
    }
}

Upvotes: 6

Views: 4814

Answers (2)

Jimmy Schementi
Jimmy Schementi

Reputation: 1239

Looks like the VS Silverlight tools generate a "zipButtonOut" accessor for x:Name resources also. In the future, just take a look at the generated file (probably "test1.g.cs") in the obj folder to see what code is generated for x:Names.

Upvotes: 0

Rich
Rich

Reputation: 36806

I don't know why it works in Silverlight, but in WPF controls you add to the resources collection are not available by their x:Name in the code behind. They're accessible through the Resources collection by their x:Key, so you can remove the x:Name attribute and add the following line of code just before the line in your code behind that is commented out, and it'll work (uncomment the line in question, of course):

Storyboard zipButtonOut = (Storyboard)Resources["zipButtonOut"];

Note that this requires the following using statement:

using System.Windows.Media.Animation;

Upvotes: 12

Related Questions