locked
Acquiring a reference to a XAML element within a style RRS feed

  • Question

  • Hello,

    I was wondering if someone could tell me how I can reference a named (x:Name) animation within a named style from code behind? For instance, I have a style called "MyStyle" with an animation called "MyAnimation" inside a control template. From this, how might I go about getting a reference to the animation from imperative code?

    Thanks,

    sfx

    Monday, January 25, 2010 2:42 AM

All replies

  •  You can get it with FindName after the Template is applied. Did you derive your own class from Control? Then override OnApplyTemplate and get it there.

    Another way is to use the VisualTreeHelper. But as far as i know it's only possible after the Template is applied to an element

    Monday, January 25, 2010 2:58 AM
  • Hi Thomas,

    Thank you very much for your swift reply.

    I need to get the reference once the XAML page has loaded (so I'm guessing the template has been applied). Could I trouble you for a code snippet demonstrating how this might be done?

     Thanks again,

     sfx

    Monday, January 25, 2010 5:22 AM
  • Unfortunately, I can't seem to get the FindName method to find the named animation within a named style - it always returns null. Does anyone know of another way to go about this?

    Cheers,

    sfx

    Tuesday, January 26, 2010 6:05 PM
  • Hi Sfx,

    Did you put it inside a VisualStateManager?   If yes, you'd better find the Storyboard like this way.  http://forums.silverlight.net/forums/p/18800/64158.aspx

    Otherwise, please share a tiny sample with us.  Thanks.

    Best regards,

    Jonathan

    Thursday, January 28, 2010 1:56 AM
  • Hi Jonathan,

    Thanks for your reply. The link you sent looked promising but unfortunately I still can't seem to get a reference to the element I want.

    What I have is a data-bound TreeView that uses the ItemContainerStyle to style each TreeViewItem/Data Item by means of a ControlTemplate. Within the template there are a series of VisualStateGroups, VisualStates and Storyboards. There is one Storyboard that has a named DoubleAnimation that I would like to change the To property of in code. The style gets applied without any issues, but every attempt to get a reference to this named DoubleAnimation fails.

    After reviewing the link you sent, I followed the direction of calling VisualStateManager.GetVisualStateGroups(this.LayoutRoot) [yes, LayoutRoot is a Grid and is my root element] but all I get is a List containing 0 items. Obviously any attempt to write a LINQ query over an empty sequence fails too.

    This problem is really doing my head in. Any further help you could provide would be appreciated.

    Cheers,

    sfx

    Sunday, January 31, 2010 7:38 PM
  • Hi Sfx,

    Would you please share a tiny repro with us?

    Best regards,

    Jonathan

    Sunday, January 31, 2010 9:16 PM
  • Hi Jonathan, I'm trying to get access to the heightAnimation in the code below. Thanks  
    <UserControl.Resources>
    <Style x:Key="treeViewItemStyle" TargetType="controls:TreeViewItem">
                <Setter Property="Padding" Value="3"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Top"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Cursor" Value="Arrow"/>
                <Setter Property="IsTabStop" Value="True"/>
                <Setter Property="TabNavigation" Value="Once"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="controls:TreeViewItem">
                            <Grid Background="{TemplateBinding Background}">
                                <vsm:VisualStateManager.VisualStateGroups>
                                    ...
                                    <vsm:VisualStateGroup x:Name="ExpansionStates">
                                        <vsm:VisualState x:Name="Collapsed">
                                            <Storyboard Duration="0:0:3">
                                                <DoubleAnimation Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="opacity" />
                                                <DoubleAnimation Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="Height"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="Expanded">                                        
                                            <Storyboard Duration="0:0:3">
                                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="Visibility">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                                </ObjectAnimationUsingKeyFrames>
                                                <DoubleAnimation Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="opacity" To="1"/>
                                                <DoubleAnimation x:Name="heightAnimation" Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="Height" To="250"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>                                
                                	</VisualStateManager.VisualStateGroups>
    					...
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModel}}" >
                    
    <controls:TreeView  	x:Name="menuTreeView" 
                              	ItemsSource="{Binding Path=TreeViewCategories}" 
                              	ItemTemplate="{StaticResource levelOneHierarchicalTemplate}"
                              	ItemContainerStyle="{StaticResource treeViewItemStyle}"
                              	Background="{StaticResource PanelGradient}" 
                              	BorderThickness="0,0,0,0"                            
                              	Margin="5" />
    </Grid>
    
     
    Sunday, January 31, 2010 10:26 PM
  • Hi Sfx,

    Sorry for the delay.   You reply was missed somehow.   To get heightAnimation, we need to first get Your VisualStateGroups by using VisualStateManager.GetVisualStateGroups(parent name); Then find the State ---> Storyboard ---> animation.  You can take a look at this link.

    Best regards,

    Jonathan


     

    Wednesday, February 3, 2010 12:56 AM
  • Hi Jonathan,

    Thanks for sending on the link. Unfortunately, when using the code you forwarded me to I always get a 0 count of VisualStateGroups when calling the method VisualStateManager.GetVisualStateGroups(this.LayoutRoot).

    Do you happen to know why this might be occurring?

    Cheers,

    sfx

    Wednesday, February 3, 2010 6:13 PM
  • Hi Sfx,

    Do you happen to know why this might be occurring?

    Ok. Please send me a runable repro and I will investigate in your shoes.   You can upload it somewhere and share the link with us.  Thanks.

    Best regards,

    Jonathan

    Wednesday, February 3, 2010 8:24 PM
  • Any answer ?

    I'm facing the same issue.. :(


    Wednesday, November 3, 2010 7:12 AM