locked
How to access the Text of a TextBox in DataTemplate in GroupedListView?

    Question

  • <DataTemplate x:Key="SampleDataTemplate">
    <Grid Margin="0,0,0,10" Tapped="DataTemplate_Tapped">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
                    <Border x:Name="templatePlaceholder" Background="#FF666666" Margin="1"/>
                    <Image x:Name="templateImage" Source="{Binding ItemImage}" />

    <Grid Grid.Column="1" Margin="10,0,0,0">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
                            <TextBlock x:Name="templateTitle" HorizontalAlignment="Left" 
                                       Text="{Binding Title}" VerticalAlignment="Top" 
                                       Margin="0,0,0,-0.167" Style="{StaticResource ListViewItemTextBlockStyle}"/>
                            <TextBlock x:Name="templateSubTitle" HorizontalAlignment="Left" TextWrapping="Wrap" 
                                       Text="{Binding SubTitle}" VerticalAlignment="Top" Margin="0" 
                                       Grid.Row="1" Style="{StaticResource ListViewItemSubheaderTextBlockStyle}"/>
                        </Grid>

    </Grid>
    </DataTemplate>







    <ListView 
                        x:Name="GroupedListView"
       ItemsSource="{Binding Source={StaticResource ItemsGroupedByGroup}}" 
       ItemTemplate="{StaticResource SampleDataTemplate}" 
                        ContinuumNavigationTransitionInfo.ExitElementContainer="True"
                        ScrollViewer.HorizontalScrollMode="Disabled"    
                        ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                        IsItemClickEnabled="True"
                        SelectionMode="None"
                        ContainerContentChanging="IncrementalUpdateHandler" Margin="0,0,0,54"
                        >
                            <!-- ContainerContentChanging="IncrementalUpdateHandler"-->
                            <ListView.GroupStyle>
                                <GroupStyle HidesIfEmpty="True" >
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <Grid Background="{StaticResource PhoneAccentBrush}" Margin="0,0,12,5" 
                                              HorizontalAlignment="Stretch" 
                                              Width="{Binding Converter={StaticResource ScreenWidth}, ConverterParameter=1}">
                                                <TextBlock Text="{Binding Key}" Margin="12,7" FontSize="24" />
                                            </Grid>
                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ListView.GroupStyle>
                        </ListView>


     I want to access the text of the TextBlock "templateTitle" and "templateSubTitle" in the function defined below. I tried all the ways but in vain.

       private void Grid_Tapped(object sender, TappedRoutedEventArgs e) {

       #Function to be written.

        }

       Can someone please help me solve this problem?



       Thanks in advance.
    Wednesday, October 29, 2014 1:53 PM

Answers

All replies

  • Hi VenuMadhav,

    Per my understanding, you should use VisualTreeHelper class to loop the XAML to find the control. Jerry Nixon has a blog talking about how to retrie control in data template. You can have a look at it and try the method.

    If it doesn’t work, please send me a repro project. I need more information for it. Use your OneDrive and share a link here. I will look into it.

    Regards,

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, October 30, 2014 8:00 AM
    Moderator
  • Thanks Herro Wong.

    That too didn't work. The link to the repo is http://1drv.ms/1vlpqop and this is the sample app for the same thing which I am using in my app. In the GroupedListView.xaml.cs, the method Grid_tapped is the place where I have to access the text stored in templateTitle and templateSubTitle of TextBlocks in SampleDatatemplate.

    Thanks again. Hoping that you might give me a solution 

    Thursday, October 30, 2014 7:36 PM
  • You're probably not in need for an answer anymore, but I found this question while looking for something similar and also just recently stumbled across an article that probably perfectly answers your question. This might be helpful for someone else later.

    How to group items in a list or grid (Windows Store apps using C#/VB/C++ and XAML)

    There's grouping of a ListView and querying with LINQ, just what you were asking for.

    Monday, November 03, 2014 7:40 AM