Windows Phone 8.1切换DataTemplate中TextBlock的可见性

时间:2014-11-07 11:15:55

标签: xaml windows-phone-8.1

我正在构建一个Windows Phone 8.1集线器应用程序。其中一个hub部分包含一个显示文章列表的ListView。我想在这个hubsection中添加一个Textblock,当文章无法下载时会显示一条消息。 XAML代码如下:

<HubSection 
    x:Uid="ArticlesSection" 
    Header="ARTICLES" 
    DataContext="{Binding Articles}" 
    HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
    <DataTemplate>
        <Grid>
            <ListView 
                AutomationProperties.AutomationId="ItemListViewSection3"
                AutomationProperties.Name="Items In Group"
                SelectionMode="None"
                IsItemClickEnabled="True"
                ItemsSource="{Binding}"
                ItemTemplate="{StaticResource BannerBackgroundArticleTemplate}"
                ItemClick="ItemView_ItemClick"
                ContinuumNavigationTransitionInfo.ExitElementContainer="True">
            </ListView>
            <TextBlock 
                x:Name="NoArticlesTextBlock" 
                HorizontalAlignment="Center" 
                VerticalAlignment="center" 
                Style="{StaticResource HeaderTextBlockStyle}" 
                TextWrapping="WrapWholeWords" 
                TextAlignment="Center"/>
        </Grid>
    </DataTemplate>
</HubSection>

我遇到的问题是我无法从C#代码访问TextBlock。有更简单的方法吗?

1 个答案:

答案 0 :(得分:10)

  

我遇到的问题是我无法从C#代码访问TextBlock。

是的,因为TextBlock是在DataTemplate中定义的,所以TextBlock在应用DataTemplate之前不可用。因此,x:Name属性不会在* .g.i.cs文件的InitializeComponent方法中自动生成变量引用。 (阅读XAML Namescopes了解更多信息)。

如果您想从代码隐藏中访问它,有两种方法:

第一种方法是最简单的:你可以在该TextBlock的Loaded事件处理程序的sender参数中获得对TextBlock的引用。

<TextBlock Loaded="NoArticlesTextBlock_Loaded" />

然后在您的代码隐藏中:

private TextBlock NoArticlesTextBlock;

private void NoArticlesTextBlock_Loaded(object sender, RoutedEventArgs e)
{
    NoArticlesTextBlock = (TextBlock)sender;
}

第二种方法是手动遍历可视树以找到具有所需名称的元素。这更适合动态布局,或者当您想要引用许多控件时,执行上一个方法会太乱。你可以这样做:

<Page Loaded="Page_Loaded" ... />

然后在您的代码隐藏中:

static DependencyObject FindChildByName(DependencyObject from, string name)
{
    int count = VisualTreeHelper.GetChildrenCount(from);

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(from, i);
        if (child is FrameworkElement && ((FrameworkElement)child).Name == name)
            return child;

        var result = FindChildByName(child, name);
        if (result != null)
            return result;
    }

    return null;
}

private TextBlock NoArticlesTextBlock;

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    // Note: No need to start searching from the root (this), we can just start
    // from the relevant HubSection or whatever. Make sure your TextBlock has
    // x:Name="NoArticlesTextBlock" attribute in the XAML.
    NoArticlesTextBlock = (TextBlock)FindChildByName(this, "NoArticlesTextBlock");
}

Jerry Nixon has a good page on his blog about this.