Using code behind to access ResourceDictionary<p align=left><font face=Arial size=2>I've been searching the web for an answer to what I think is a simple problem.  I have placed a button in a resource dictionary with several storyboards.  Two of those storyboards are &quot;FullLight&quot; and &quot;LowLight&quot;.  The button is named AppButton.  So, on the resource dictionary you would see:</font></p> <p align=left> </p> <p align=left>&lt;Style x:Key=&quot;AppButton&quot; BasedOn=&quot;{x:Null}&quot; TargetType=&quot;{x:Type Button}&quot;&gt;</p> <p align=left>...</p> <p align=left>&lt;Storyboard x:Key=&quot;FullLight&quot;&gt;</p> <p align=left>...</p> <p align=left>&lt;Storyboard x:Key=&quot;LowLight&quot;&gt;</p> <p align=left>...</p> <p> </p> <p align=left>This is in a file called MyButtons.xaml</p> <p align=left> </p> <p align=left>In &quot;Window1.xaml&quot;, here is the code for the buttons:</p> <p align=left> </p> <p align=left>    &lt;Button Click=&quot;btnRollForward_Clicked&quot; RenderTransformOrigin=&quot;0.5,0.5&quot; HorizontalAlignment=&quot;Left&quot; x:Name=&quot;btnRollForward&quot; Style=&quot;{DynamicResource AppButton}&quot; Width=&quot;125&quot; Content=&quot;RollForward&quot; Foreground=&quot;#FFFFFFFF&quot;&gt;<br>    ...<br>    &lt;/Button&gt;<br>    &lt;Button RenderTransformOrigin=&quot;0.5,0.5&quot; x:Name=&quot;btnTransfer&quot; Style=&quot;{DynamicResource AppButton}&quot; Width=&quot;125&quot; Content=&quot;TransferThisFile&quot; Foreground=&quot;#FFFFFFFF&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;129,0,0,0&quot;&gt;<br>     ...<br>    &lt;/Button&gt;<br></p> <p align=left>Now, I am attempting to have a click event for btnRollForward fire the &quot;LowLight&quot; storyboard for btnTransfer.  In other words, I would like the button named &quot;btnTransfer&quot; to darken when &quot;btnRollForward&quot; is clicked.</p> <p align=left> </p> <p align=left>In Window1.xaml.cs, I have wired up:</p> <p align=left> </p> <p align=left>private void btnRollForward_Clicked(object sender, EventArgs e)<br>        {<br>            // Insert code here that will fire the &quot;LowLight&quot; storyboard for btnTransfer               <br>        }</p> <p align=left> </p> <p align=left>I have looked everywhere on the internet and cannot get past this simple problem.  My main issue is:  How do I get the storyboard event named &quot;LowLight&quot; specifically for btnTransfer to fire?  Trial and error has been unsuccessful so far.  Any help would be appreciated.</p>© 2009 Microsoft Corporation. All rights reserved.Sat, 27 Sep 2008 00:29:45 Ze81b671c-2715-4851-a76b-621029e16a16http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#e81b671c-2715-4851-a76b-621029e16a16http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#e81b671c-2715-4851-a76b-621029e16a16Opie1313http://social.msdn.microsoft.com/Profile/en-US/?user=Opie1313Using code behind to access ResourceDictionary<p align=left><font face=Arial size=2>I've been searching the web for an answer to what I think is a simple problem.  I have placed a button in a resource dictionary with several storyboards.  Two of those storyboards are &quot;FullLight&quot; and &quot;LowLight&quot;.  The button is named AppButton.  So, on the resource dictionary you would see:</font></p> <p align=left> </p> <p align=left>&lt;Style x:Key=&quot;AppButton&quot; BasedOn=&quot;{x:Null}&quot; TargetType=&quot;{x:Type Button}&quot;&gt;</p> <p align=left>...</p> <p align=left>&lt;Storyboard x:Key=&quot;FullLight&quot;&gt;</p> <p align=left>...</p> <p align=left>&lt;Storyboard x:Key=&quot;LowLight&quot;&gt;</p> <p align=left>...</p> <p> </p> <p align=left>This is in a file called MyButtons.xaml</p> <p align=left> </p> <p align=left>In &quot;Window1.xaml&quot;, here is the code for the buttons:</p> <p align=left> </p> <p align=left>    &lt;Button Click=&quot;btnRollForward_Clicked&quot; RenderTransformOrigin=&quot;0.5,0.5&quot; HorizontalAlignment=&quot;Left&quot; x:Name=&quot;btnRollForward&quot; Style=&quot;{DynamicResource AppButton}&quot; Width=&quot;125&quot; Content=&quot;RollForward&quot; Foreground=&quot;#FFFFFFFF&quot;&gt;<br>    ...<br>    &lt;/Button&gt;<br>    &lt;Button RenderTransformOrigin=&quot;0.5,0.5&quot; x:Name=&quot;btnTransfer&quot; Style=&quot;{DynamicResource AppButton}&quot; Width=&quot;125&quot; Content=&quot;TransferThisFile&quot; Foreground=&quot;#FFFFFFFF&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;129,0,0,0&quot;&gt;<br>     ...<br>    &lt;/Button&gt;<br></p> <p align=left>Now, I am attempting to have a click event for btnRollForward fire the &quot;LowLight&quot; storyboard for btnTransfer.  In other words, I would like the button named &quot;btnTransfer&quot; to darken when &quot;btnRollForward&quot; is clicked.</p> <p align=left> </p> <p align=left>In Window1.xaml.cs, I have wired up:</p> <p align=left> </p> <p align=left>private void btnRollForward_Clicked(object sender, EventArgs e)<br>        {<br>            // Insert code here that will fire the &quot;LowLight&quot; storyboard for btnTransfer               <br>        }</p> <p align=left> </p> <p align=left>I have looked everywhere on the internet and cannot get past this simple problem.  My main issue is:  How do I get the storyboard event named &quot;LowLight&quot; specifically for btnTransfer to fire?  Trial and error has been unsuccessful so far.  Any help would be appreciated.</p>Sat, 01 Sep 2007 23:05:26 Z2007-09-06T03:49:43Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#a92d9528-a777-47fa-9533-62a377d5588ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#a92d9528-a777-47fa-9533-62a377d5588eDr. WPFhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dr.%20WPFUsing code behind to access ResourceDictionary<p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left> </p> <p align=left>Storyboard lowLight = btnTransfer.FindResource(&quot;LowLight&quot;) as Storyboard;</p> <p align=left>if (lowLight != null)</p> <p align=left>{</p> <p align=left>    lowLight.Begin(btnTransfer);</p> <p align=left>}</p> <p align=left> </p></div></div> <p align=left> </p> <p></p> <p align=left>Note that this asumes you have already set the Storyboard.TargetName and Storyboard.TargetProperty attached properties on the animations within your storyboard.</p> <p align=left><font face=Arial size=2></font> </p>Sat, 01 Sep 2007 23:42:15 Z2007-09-06T03:49:43Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#e12fa790-1d49-46fd-b02a-c245ef5927fahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#e12fa790-1d49-46fd-b02a-c245ef5927faOpie1313http://social.msdn.microsoft.com/Profile/en-US/?user=Opie1313Using code behind to access ResourceDictionary<p align=left><font face=Arial size=2>Thank you very much for replying.  I certainly agree with your answer.  </font></p> <p align=left> </p> <p align=left>I fully expected it to work, but have run into this error:</p> <p align=left>'rectangle' name cannot be found in the name scope of 'System.Windows.Controls.Button'</p> <p> </p> <p align=left>'rectangle' refers to the &lt;Rectangle&gt; object that serves as the button in appbutton.</p> <p align=left> </p> <p align=left>I have spent a day trying out different workarounds.</p> <p align=left> </p> <p align=left>I am using Visual Studio 2005 and Blend version 1.01.  Am I running into a Microsoft bug?</p> <p align=left> </p> <p align=left>When the other events on the button are triggered, everything works fine.  It is only when I press the btnRollForward that I get an error.</p> <p align=left> </p> <p align=left>Here is more of the code from MyButtons.xaml</p> <p align=left> </p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>&lt;Style x:Key=&quot;AppButton&quot; BasedOn=&quot;{x:Null}&quot; TargetType=&quot;{x:Type Button}&quot;&gt;<br>  &lt;Setter Property=&quot;Template&quot;&gt;<br>   &lt;Setter.Value&gt;<br>    &lt;ControlTemplate TargetType=&quot;{x:Type Button}&quot;&gt;<br>     &lt;ControlTemplate.Resources&gt;<br>      &lt;Storyboard x:Key=&quot;LowLight&quot;&gt;<br>       &lt;ColorAnimationUsingKeyFrames BeginTime=&quot;00:00:00&quot; Duration=&quot;00:00:00.0010000&quot;          Storyboard.TargetName=&quot;rectangle&quot; Storyboard.TargetProperty=&quot;(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)&quot;&gt;</p></blockquote> <p align=left>          ......</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>&lt;Storyboard x:Key=&quot;FullLight&quot;&gt;<br>       &lt;ColorAnimationUsingKeyFrames BeginTime=&quot;00:00:00&quot; Storyboard.TargetName=&quot;rectangle&quot; Storyboard.TargetProperty=&quot;(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)&quot;&gt;<br>.....</p> <p align=left>&lt;Grid&gt;<br>      &lt;Rectangle Visibility=&quot;Visible&quot; Stroke=&quot;#FFFAF4F4&quot; StrokeThickness=&quot;0&quot; x:Name=&quot;rectangle&quot;&gt;<br>       &lt;Rectangle.Fill&gt;<br>        &lt;LinearGradientBrush EndPoint=&quot;0.512,1&quot; StartPoint=&quot;0.512,0.785&quot;&gt;<br>         &lt;GradientStop Color=&quot;#FF000000&quot; Offset=&quot;0&quot;/&gt;<br>         &lt;GradientStop Color=&quot;#FFFEEAA9&quot; Offset=&quot;0.986&quot;/&gt;<br>        &lt;/LinearGradientBrush&gt;<br>       &lt;/Rectangle.Fill&gt;<br>      &lt;/Rectangle&gt;<br>      &lt;ContentPresenter SnapsToDevicePixels=&quot;{TemplateBinding SnapsToDevicePixels}&quot; HorizontalAlignment=&quot;{TemplateBinding HorizontalContentAlignment}&quot; VerticalAlignment=&quot;{TemplateBinding    VerticalContentAlignment}&quot; RecognizesAccessKey=&quot;True&quot;/&gt;<br>&lt;/Grid&gt;<br>     &lt;ControlTemplate.Triggers&gt;<br>      &lt;Trigger Property=&quot;IsFocused&quot; Value=&quot;True&quot;/&gt;<br>      &lt;Trigger Property=&quot;IsDefaulted&quot; Value=&quot;True&quot;/&gt;<br>      &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;<br>       &lt;Trigger.EnterActions&gt;<br>        &lt;BeginStoryboard x:Name=&quot;LowLight_BeginStoryboard&quot; Storyboard=&quot;{StaticResource LowLight}&quot;/&gt;<br>       &lt;/Trigger.EnterActions&gt;<br>       &lt;Trigger.ExitActions&gt;<br>        &lt;BeginStoryboard x:Name=&quot;Darken_BeginStoryboard&quot; Storyboard=&quot;{StaticResource Darken}&quot;/&gt;<br>       &lt;/Trigger.ExitActions&gt;<br>      &lt;/Trigger&gt;<br>      &lt;Trigger Property=&quot;IsPressed&quot; Value=&quot;True&quot;&gt;<br>       &lt;Trigger.EnterActions&gt;<br>        &lt;BeginStoryboard x:Name=&quot;FullLight_BeginStoryboard&quot; Storyboard=&quot;{StaticResource FullLight}&quot;/&gt;<br>       &lt;/Trigger.EnterActions&gt;<br>      &lt;/Trigger&gt;<br>      &lt;Trigger Property=&quot;IsEnabled&quot; Value=&quot;False&quot;/&gt;<br>     &lt;/ControlTemplate.Triggers&gt;<br>    &lt;/ControlTemplate&gt;<br>   &lt;/Setter.Value&gt;<br>  &lt;/Setter&gt;<br> &lt;/Style&gt;</p> <p align=left> </p> <p align=left>... the wired up event:</p> <p align=left> </p> <p align=left>private void btnRollForward_Clicked(object sender, EventArgs e)<br>        {<br>            Storyboard lowLight = btnTransfer.FindResource(&quot;LowLight&quot;) as Storyboard;<br>            if (lowLight != null)<br>            {<br>                lowLight.Begin(btnTransfer);<br>            }<br>               <br>        }</p></blockquote> <p align=left> </p> <p align=left>             Thanks again.</p> <p align=left> </p> <p align=left>Any help on this problem would be appreciated.</p>Sun, 02 Sep 2007 20:26:53 Z2007-09-02T20:26:53Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#cdce1c89-905f-4f84-be17-2cdcf2295c36http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#cdce1c89-905f-4f84-be17-2cdcf2295c36Dr. WPFhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dr.%20WPFUsing code behind to access ResourceDictionaryAhhh... the storyboard resides within your control template.  That's a very important piece of information. <p align=left><font face=Arial size=2></font> </p> <p>In that case, you should be able to do this:</p> <p align=left> </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left> </p> <p align=left>Storyboard lowLight = btnTransfer.Template.FindResource(&quot;LowLight&quot;) as Storyboard;</p> <p align=left>if (lowLight != null)</p> <p align=left>{</p> <p align=left>    lowLight.Begin(btnTransfer);</p> <p align=left>}</p> <p align=left> </p></div></div> <p align=left> </p> <p></p> <p>It's simply a matter of namescope.  </p> <p align=left> </p> <p align=left>You could also do the following, in your scenario, to avoid the resource resolution</p> <p> </p> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left> </p> <p align=left>Storyboard lowLight = btnTransfer.Template.Resources[&quot;LowLight&quot;] as Storyboard;</p> <p align=left>if (lowLight != null)</p> <p align=left>{</p> <p align=left>    lowLight.Begin(btnTransfer);</p> <p align=left>}</p> <p align=left> </p></div>Sun, 02 Sep 2007 21:20:27 Z2007-09-06T03:49:18Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#9ef5a14b-1015-470e-8028-47a3c8ee6178http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#9ef5a14b-1015-470e-8028-47a3c8ee6178Opie1313http://social.msdn.microsoft.com/Profile/en-US/?user=Opie1313Using code behind to access ResourceDictionary<p align=left><font face=Arial size=2>Thanks for the help</font></p>Tue, 11 Sep 2007 01:38:18 Z2007-09-11T01:38:18Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#02b21470-47bf-44d6-b381-f49c34921b80http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e81b671c-2715-4851-a76b-621029e16a16#02b21470-47bf-44d6-b381-f49c34921b80Cory Plottshttp://social.msdn.microsoft.com/Profile/en-US/?user=Cory%20PlottsUsing code behind to access ResourceDictionaryYeah, thanks ... this helped me too:<br><a href="http://cplotts.wordpress.com/2008/09/26/dr-wpf-namescopes/">http://cplotts.wordpress.com/2008/09/26/dr-wpf-namescopes/</a><br><br> Sat, 27 Sep 2008 00:29:45 Z2008-09-27T00:29:45Z