Click here to Skip to main content
15,848,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I have a StackPanel and a StoryBoard in xaml code
XML
<Window x:Class="WpfApp_ColourTextBlock.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <System:String x:Key="textBlockName">textBlock0</System:String>
        <Storyboard x:Key="Storyboard1">
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="{DynamicResource textBlockName}">
                <EasingColorKeyFrame KeyTime="0:0:2.5" Value="Red"/>
                <EasingColorKeyFrame KeyTime="0:0:5" Value="Blue"/>
                <EasingColorKeyFrame KeyTime="0:0:7.5" Value="Red"/>
                <EasingColorKeyFrame KeyTime="0:0:10" Value="White"/>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Grid>
        <StackPanel Name="TextBlocks" Orientation="Horizontal">

        </StackPanel>
    </Grid>
</Window>


and I create a TextBlock in C# code behind at runtime
C#
public MainWindow()
{
    InitializeComponent();

        TextBlock tb = new TextBlock();
        tb.Uid = "textBlock1";
        tb.Text = "test";
        TextBlocks.Children.Add(tb);
        Resources["textBlockName"] = "textBlock1";
        (Resources["Storyboard1"] as Storyboard).Begin();
 }


But happened this error:
C#
'textBlock0' name cannot be found in the name scope of 'WpfApp_ColourTextBlock.MainWindow'.


Is solution for set TargetName at runtime?

Thanks in advance.
Posted
Updated 25-Jul-12 8:29am
v3
Comments
Sandeep Mewara 25-Jul-12 13:53pm    
Where is textBlock0 defined?
hzawary 25-Jul-12 13:57pm    
Nowhere, it is just a value string for 'textBlockName'.
hzawary 25-Jul-12 14:26pm    
I'm wait and so tired then came back tomorrow:P

1 solution

Need to register the name:

myMainPanel.RegisterName(button1.Name, button1);


See http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.registername.aspx[^]
 
Share this answer
 
Comments
hzawary 26-Jul-12 11:38am    
My 5:) for nice job!
Clifford Nelson 26-Jul-12 13:49pm    
Thanks very much. There was another question on this, and I answered it, but forgot the answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900