Search code examples
c#windows-phonewindows-store-appswindows-phone-8.1winrt-xaml

How to access child elements of HubView XAML in C#?


I've just moved from Windows Phone 8.1 Silverlight to Windows Phone Store apps. I've the following XAML for an app page:

<Page
    x:Class="WebClip.HubPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WebClip"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:WebClip.Data"
    mc:Ignorable="d">

    <Grid x:Name="LayoutRoot">
        <Hub x:Name="Hub"
             Header="web clip"
             Background="{ThemeResource HubBackgroundImageBrush}">

            <HubSection x:Name="TileSelectorView"
                        Header="TILE SELECTOR">
                <DataTemplate>
                    <ListView x:Name="TileList"/>
                </DataTemplate>
            </HubSection>

            <HubSection x:Name="BrowserView"
                        Header="BROWSER">
                <DataTemplate>
                    <WebView x:Name="BrowserBox"/>
                </DataTemplate>
            </HubSection>

        </Hub>
    </Grid>
</Page>

Earlier, in Silverlight, I can access an element like TileList directly to do something like:

TileList.ItemsSource = <SomeItemSourceList>;

But now I'm unable to do that in the backend C# code. The TileList in itself is not accessible. However, TileSelectorView and BrowserView (ref: code above) are accessible.

I found these two questions where Jerry had answered to something similar:

  1. How to accessing Elements in XAML DataTemplate Listview without interacting with it
  2. How do I access a control inside a XAML DataTemplate?

However, I'm not able to replicate them. There is no Items property under my TileSelectorView to iterate through.

What am I doing wrong? How do I get about this?


Solution

  • I ran into the same issue when moving from windows Phone Silverlight to Windows Phone RT. You can get around this issue by data binding as the others suggested but sometime you want to just get the control on the page. I found the following article quite helpful..

    Get the controls inside DataTemplate control