【问题标题】:How to Access Grid View from hub section Data Template Windows phone 8.1 in C#如何在 C# 中从集线器部分数据模板 Windows phone 8.1 访问网格视图
【发布时间】:2015-11-10 17:06:18
【问题描述】:

请告诉我如何评估中心部分数据模板中的网格视图。 在通用应用程序中(Windows Phone 8.1) 以下是 xaml

   <HubSection Header="English Newspapers" x:Name="HubEnglish">
        <DataTemplate x:Name="DTEnglish">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Vertical">
                    <GridView x:Name="LstEnglishNewspapers">
                        <GridView.ItemTemplate>
                            <DataTemplate>

                                <StackPanel Margin="0,0,0,0" Orientation="Horizontal">

                                    <Image x:Name="txtNewspaperImage" Source="{Binding PicPath}" Height="60" Width="60" VerticalAlignment="Center" Margin="0,0,10,0"/>
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock x:Name="txtNewspaperHeader" TextWrapping="Wrap" Foreground="{StaticResource ApplicationForegroundThemeBrush}" FontSize="14.667" FontWeight="Light" Width="200" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI"/>
                                        <TextBlock x:Name="txtNewsHeader" TextWrapping="Wrap" Foreground="{StaticResource ApplicationForegroundThemeBrush}" FontSize="14.667" FontWeight="Light" Width="200" MaxHeight="20" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                                    </StackPanel>

                                </StackPanel>
                            </DataTemplate>

                        </GridView.ItemTemplate>
                    </GridView>

                </StackPanel>
            </Grid>
        </DataTemplate>

    </HubSection>

我已尝试使用此代码获取网格,但出现异常。

var grid = VisualTreeHelper.GetChild(HubEnglish.ContentTemplate, 0);

异常:灾难性故障(HRESULT 异常:0x8000FFFF (E_UNEXPECTED))

【问题讨论】:

  • 好吧,这将有助于显示您也得到了什么例外......你不觉得......吗?
  • @DJKRAZE 我已经更新了例外的问题。请看一下
  • this answer
  • 有没有其他解决办法

标签: c# xaml windows-phone-8.1 win-universal-app


【解决方案1】:

找到这段代码是为了获取数据模板

    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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多