This documentation is archived and is not being maintained.

FrameworkElement.RegisterName Method

Provides an accessor that simplifies access to the NameScope registration method.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

public void RegisterName(
	string name,
	Object scopedElement


Type: System.String
Name to use for the specified name-object mapping.
Type: System.Object
Object for the mapping.

This method is a convenience method for calling RegisterName. The implementation will check successive parent elements until it finds the applicable NameScope implementation, which is found by finding an element that implements INameScope. For more information about namescopes, see WPF XAML Namescopes.

Calling RegisterName is necessary in order to correctly hook up animation storyboards for applications when created in code. This is because one of the key storyboard properties, TargetName, uses a run-time name lookup instead of being able to take a reference to a target element. This is true even if that element is accessible by reference from the code. For more information on why you need to register names for storyboard targets, see Storyboards Overview.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Microsoft.Samples.Animation.AnimatingWithStoryboards

    public class ScopeExample : Page

        private Storyboard myStoryboard;
        private StackPanel myMainPanel;
        private Button button1, button2;

        public ScopeExample()

            this.Background = Brushes.White; 
            myMainPanel = new StackPanel();

            // Create a name scope for the stackpanel. 
            NameScope.SetNameScope(myMainPanel, new NameScope());

            myMainPanel.Background = Brushes.Orange;

            button1 = new Button();
            button1.Name = "Button1";

            // Register button1's name with myMainPanel.
            myMainPanel.RegisterName(button1.Name, button1);
            button1.Content = "Button 1";
            button1.Click += new RoutedEventHandler(button1Clicked);

            button2 = new Button();
            button2.Name = "Button2";

            // Register button2's name with myMainPanel.
            myMainPanel.RegisterName(button2.Name, button2);
            button2.Content = "Button 2";
            button2.Click += new RoutedEventHandler(button2Clicked);

            // Create some animations and a storyboard.
            DoubleAnimation button1WidthAnimation = 
                new DoubleAnimation(300, 200, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTargetName(button1WidthAnimation, button1.Name);
            Storyboard.SetTargetProperty(button1WidthAnimation, new PropertyPath(Button.WidthProperty));

            DoubleAnimation button2WidthAnimation = 
                new DoubleAnimation(300, 200, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTargetName(button2WidthAnimation, button2.Name);
            Storyboard.SetTargetProperty(button2WidthAnimation, new PropertyPath(Button.WidthProperty));

            DoubleAnimation heightAnimationWithoutTarget = 
                new DoubleAnimation(300, 200, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTargetProperty(heightAnimationWithoutTarget, new PropertyPath(FrameworkElement.HeightProperty));                

            myStoryboard = new Storyboard();

            this.Content = myMainPanel;


        private void button1Clicked(object sender, RoutedEventArgs args)

            // Starts the animations. The animation without a specified 
            // target name, heightAnimationWithoutTarget, is applied to
            // myMainPanel.


        private void button2Clicked(object sender, RoutedEventArgs args)

            // Starts the animations. The animation without a specified 
            // target name, heightAnimationWithoutTarget, is applied to
            // button2.




.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.