locked
How to get control name inside data template? RRS feed

  • Question

  • HI

    I have a data template for a gridview here-

    <DataTemplate x:Key="StoryItemsTemplate">
                <Grid HorizontalAlignment="Left" Width="320" Height="180">
                    <Grid.Resources>
                        <Storyboard x:Name="storyItemsStoryboard">
                            <DoubleAnimation Storyboard.TargetName="StoryItemsRectangle" Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:5"/>
                        </Storyboard>
                    </Grid.Resources>              
                    <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                        <Image Source="{Binding StoryImage}" Stretch="UniformToFill" AutomationProperties.Name="{Binding StoryTitle}"/>
                    </Border>
                    <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                        <TextBlock x:Name="tblTitle" Text="{Binding StoryTitle}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="60" Margin="15,0,15,0"/>
                        <TextBlock Text="{Binding StorySummary}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
                    </StackPanel>

                    <Rectangle Loaded="StoryItemsRectangle_Loaded" x:Name="StoryItemsRectangle" Height="180"  Width="320" Fill="Yellow" />
                </Grid>
            </DataTemplate>


    I need to access storyboard name (storyItemsStoryboard) which inside grid.resource in page.cs



    sandeep chauhan

    Thursday, October 17, 2013 7:54 AM

Answers

  • HI Zakker,

    Big thanks for your reply.

    I have done which I actually need from XAML part.

    thanks.


    sandeep chauhan

    Friday, October 18, 2013 9:39 AM

All replies

  • Hi ,

    Have you tried something like the below ?

       foreach (Storyboard tb in FindVisualChildren<Storyboard>(this.MyGridViewName))
                {
                  if (tb.Name == your code

                }

    private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj == null)
                {
                    yield break;
                }

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    var child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (var childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }

    the tb variable is your control.

    Careful with the findvisual method because the gridview must be loaded in order to return something. Perhaps you might want to do something like

    mygridview.LayoutUpdated += mygridview_Layoutupdated;

    void mygridview_LayoutUpdated(object sender, object e)
            {
                mygridview.LayoutUpdated -= mygridview_Layoutupdated;
    // do you search HERE with the loop

            }

    Or you can use the loaded event of the Gridview

    thank you


    • Edited by zakkar Thursday, October 17, 2013 8:17 AM
    Thursday, October 17, 2013 8:13 AM
  • Hi Zakker

    Thanks for the reply.

    I tried to write this code

     foreach (Storyboard tb in FindVisualChildren<Storyboard>(this.MyGridViewName))
                {
                  if (tb.Name == your code

                }

    in gridview loaded event or updatedLayout event but its not going to inside this foreach loop.


    sandeep chauhan

    Thursday, October 17, 2013 8:51 AM
  • Hi Sandeep_chauhan,

    Hmmm. Let's try something different. 

    Put a button somewhere in your page , we don't care  just for testing purposes, let the page open and then in the clicked event of the button try and do the loop.

    Does it get inside ? Are you getting any values ?

    thank you

    Thursday, October 17, 2013 1:22 PM
  • To selectively run an animation within a storyboard of a data template item, I took the approach of using a visual state group with two states "Normal"/"Animate". I then set these through data binding to a property on the object I was data binding to. I appreciate this is not a prescriptive answer but the rough solution I came up with and approach that works nicely as an alternative approach.
    Thursday, October 17, 2013 8:14 PM
  • HI Zakker,

    Big thanks for your reply.

    I have done which I actually need from XAML part.

    thanks.


    sandeep chauhan

    Friday, October 18, 2013 9:39 AM