Accessing elements inside a datatemplate:

Category: windows metro apps csharpvb

Question

Santosh Umarani on Mon, 09 Apr 2012 10:34:04


Hi,

I've the following XAML and want to read Data template in code behind files. I have tried using FindName() but could not get the control name "ItemGroupButton" which is inside Data template. So can anybody please help me to get the control name in the code behind file.

<ScrollViewer
            x:Name="itemGridScrollViewer"
            AutomationProperties.AutomationId="ItemGridScrollViewer"
            Grid.Row="1"
            Margin="0,-3,0,0"
            Style="{StaticResource HorizontalScrollViewerStyle}">
            <GridView
                x:Name="itemGridView"
                AutomationProperties.AutomationId="ItemGridView"
                AutomationProperties.Name="Grouped Items"
                Margin="116,0,40,46"
                ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
                ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
                SelectionMode="None"
                IsItemClickEnabled="True"
                ItemClick="ItemView_ItemClick" >
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate x:Name="itemTemplatetest">
                        <VirtualizingStackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
                <GridView.GroupStyle>
                    <GroupStyle x:Name="itemGroupStyle">
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate x:Name="itemDataTemplate" >
                                <Grid Margin="1,0,0,6" x:Name="itemGrid">
                                    <Button x:Name="ItemGroupButton"
                                        AutomationProperties.Name="Group Title"
                                        Content="{Binding Title}"
                                        Click="Header_Click"
                                        Style="{StaticResource TextButtonStyle}"/>
                                </Grid>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </GridView.GroupStyle>
            </GridView>           
        </ScrollViewer>

Thanks,

Santosh

Replies

Rob Caplan [MSFT] on Tue, 10 Apr 2012 01:52:19


What are you trying to achieve with this?

Remember that there will be multiple instances of the ItemGroupButton since the template is used multiple times. Which one are you trying to manipulate and from where are you getting that information?

You will likely be better able to achieve your goal with a DataTemplateSelector or with Visual States, but without knowing what you are trying to do it is hard to make good suggestions.

--Rob