locked
Using code behind to access ResourceDictionary RRS feed

  • Question

  • 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 "FullLight" and "LowLight".  The button is named AppButton.  So, on the resource dictionary you would see:

     

    <Style x:Key="AppButton" BasedOn="{x:Null}" TargetType="{x:Type Button}">

    ...

    <Storyboard x:Key="FullLight">

    ...

    <Storyboard x:Key="LowLight">

    ...

     

    This is in a file called MyButtons.xaml

     

    In "Window1.xaml", here is the code for the buttons:

     

        <Button Click="btnRollForward_Clicked" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" x:Name="btnRollForward" Style="{DynamicResource AppButton}" Width="125" Content="RollForward" Foreground="#FFFFFFFF">
        ...
        </Button>
        <Button RenderTransformOrigin="0.5,0.5" x:Name="btnTransfer" Style="{DynamicResource AppButton}" Width="125" Content="TransferThisFile" Foreground="#FFFFFFFF" HorizontalAlignment="Left" Margin="129,0,0,0">
         ...
        </Button>

    Now, I am attempting to have a click event for btnRollForward fire the "LowLight" storyboard for btnTransfer.  In other words, I would like the button named "btnTransfer" to darken when "btnRollForward" is clicked.

     

    In Window1.xaml.cs, I have wired up:

     

    private void btnRollForward_Clicked(object sender, EventArgs e)
            {
                // Insert code here that will fire the "LowLight" storyboard for btnTransfer               
            }

     

    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 "LowLight" specifically for btnTransfer to fire?  Trial and error has been unsuccessful so far.  Any help would be appreciated.

    Saturday, September 1, 2007 11:05 PM

Answers

  • Code Snippet

     

    Storyboard lowLight = btnTransfer.FindResource("LowLight") as Storyboard;

    if (lowLight != null)

    {

        lowLight.Begin(btnTransfer);

    }

     

     

    Note that this asumes you have already set the Storyboard.TargetName and Storyboard.TargetProperty attached properties on the animations within your storyboard.

     

    Saturday, September 1, 2007 11:42 PM
  • Ahhh... the storyboard resides within your control template.  That's a very important piece of information.

     

    In that case, you should be able to do this:

     

    Code Snippet

     

    Storyboard lowLight = btnTransfer.Template.FindResource("LowLight") as Storyboard;

    if (lowLight != null)

    {

        lowLight.Begin(btnTransfer);

    }

     

     

    It's simply a matter of namescope. 

     

    You could also do the following, in your scenario, to avoid the resource resolution

     

    Code Snippet

     

    Storyboard lowLight = btnTransfer.Template.Resources["LowLight"] as Storyboard;

    if (lowLight != null)

    {

        lowLight.Begin(btnTransfer);

    }

     

    Sunday, September 2, 2007 9:20 PM

All replies

  • Code Snippet

     

    Storyboard lowLight = btnTransfer.FindResource("LowLight") as Storyboard;

    if (lowLight != null)

    {

        lowLight.Begin(btnTransfer);

    }

     

     

    Note that this asumes you have already set the Storyboard.TargetName and Storyboard.TargetProperty attached properties on the animations within your storyboard.

     

    Saturday, September 1, 2007 11:42 PM
  • Thank you very much for replying.  I certainly agree with your answer. 

     

    I fully expected it to work, but have run into this error:

    'rectangle' name cannot be found in the name scope of 'System.Windows.Controls.Button'

     

    'rectangle' refers to the <Rectangle> object that serves as the button in appbutton.

     

    I have spent a day trying out different workarounds.

     

    I am using Visual Studio 2005 and Blend version 1.01.  Am I running into a Microsoft bug?

     

    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.

     

    Here is more of the code from MyButtons.xaml

     

    <Style x:Key="AppButton" BasedOn="{x:Null}" TargetType="{x:Type Button}">
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
         <ControlTemplate.Resources>
          <Storyboard x:Key="LowLight">
           <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"          Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">

              ......

    <Storyboard x:Key="FullLight">
           <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
    .....

    <Grid>
          <Rectangle Visibility="Visible" Stroke="#FFFAF4F4" StrokeThickness="0" x:Name="rectangle">
           <Rectangle.Fill>
            <LinearGradientBrush EndPoint="0.512,1" StartPoint="0.512,0.785">
             <GradientStop Color="#FF000000" Offset="0"/>
             <GradientStop Color="#FFFEEAA9" Offset="0.986"/>
            </LinearGradientBrush>
           </Rectangle.Fill>
          </Rectangle>
          <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding    VerticalContentAlignment}" RecognizesAccessKey="True"/>
    </Grid>
         <ControlTemplate.Triggers>
          <Trigger Property="IsFocused" Value="True"/>
          <Trigger Property="IsDefaulted" Value="True"/>
          <Trigger Property="IsMouseOver" Value="True">
           <Trigger.EnterActions>
            <BeginStoryboard x:Name="LowLight_BeginStoryboard" Storyboard="{StaticResource LowLight}"/>
           </Trigger.EnterActions>
           <Trigger.ExitActions>
            <BeginStoryboard x:Name="Darken_BeginStoryboard" Storyboard="{StaticResource Darken}"/>
           </Trigger.ExitActions>
          </Trigger>
          <Trigger Property="IsPressed" Value="True">
           <Trigger.EnterActions>
            <BeginStoryboard x:Name="FullLight_BeginStoryboard" Storyboard="{StaticResource FullLight}"/>
           </Trigger.EnterActions>
          </Trigger>
          <Trigger Property="IsEnabled" Value="False"/>
         </ControlTemplate.Triggers>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>

     

    ... the wired up event:

     

    private void btnRollForward_Clicked(object sender, EventArgs e)
            {
                Storyboard lowLight = btnTransfer.FindResource("LowLight") as Storyboard;
                if (lowLight != null)
                {
                    lowLight.Begin(btnTransfer);
                }
                   
            }

     

                 Thanks again.

     

    Any help on this problem would be appreciated.

    Sunday, September 2, 2007 8:26 PM
  • Ahhh... the storyboard resides within your control template.  That's a very important piece of information.

     

    In that case, you should be able to do this:

     

    Code Snippet

     

    Storyboard lowLight = btnTransfer.Template.FindResource("LowLight") as Storyboard;

    if (lowLight != null)

    {

        lowLight.Begin(btnTransfer);

    }

     

     

    It's simply a matter of namescope. 

     

    You could also do the following, in your scenario, to avoid the resource resolution

     

    Code Snippet

     

    Storyboard lowLight = btnTransfer.Template.Resources["LowLight"] as Storyboard;

    if (lowLight != null)

    {

        lowLight.Begin(btnTransfer);

    }

     

    Sunday, September 2, 2007 9:20 PM
  • Thanks for the help

    Tuesday, September 11, 2007 1:38 AM
  • Saturday, September 27, 2008 12:29 AM