c# - 如何在 UWP 的 Pivot 中获取 Pivot.ItemTemplate 中的控件?

标签 c# .net uwp

我有以下代码:

 <ScrollViewer x:Name="swipeBetweenPages" Grid.Row="1">
                    <Pivot DataContext="{StaticResource ViewModel}" x:Name="pivot" Margin="0,-45,0,0" 
                                     HeaderTemplate="{StaticResource headerTest}" 
                           ItemTemplate="{StaticResource pivotTemplate}" ItemsSource="{Binding Articles}" SelectionChanged="pivot_SelectionChanged">
                    </Pivot>
                </ScrollViewer>

<Page.Resources>
        <ViewModels:ArticleViewModel x:Key="ViewModel" />
        <DataTemplate x:Key="headerTest">
        </DataTemplate>
        <DataTemplate x:Key="pivotTemplate">
            <StackPanel Margin="-15 0 -15 0">
                <Grid>
                    <Grid.Background>
                        <ImageBrush AlignmentX="Center" AlignmentY="Center" ImageSource="Assets/PlaceHolder.jpg"></ImageBrush>
                    </Grid.Background>
                    <Image q42controls:ImageExtensions.CacheUri="{Binding ImageURL}" Tag="{Binding ImageURL}" Tapped="ImageView"></Image>
                </Grid>
                <StackPanel Background="White">
                    <TextBlock x:Name="HeadLine" Text="{Binding HeadLine}"  
                                               Margin="10 5 0 -5" TextWrapping="Wrap" 
                                               FontSize="20" Foreground="Black"
                                               FontFamily="{StaticResource HeadlineCommonFamiy}"
                                               Pivot.SlideInAnimationGroup="GroupTwo" Height="63"
                                               FontWeight="Bold" TextTrimming="CharacterEllipsis"/>
                    <TextBlock Text="{Binding Abstract}" TextWrapping="Wrap" FontSize="15" FontStyle="Italic"
                                   Pivot.SlideInAnimationGroup="GroupTwo" Margin="10 5 0 10"
                                           FontFamily="{StaticResource AbstractCommonFamily}"/>                   
                </StackPanel>
                <StackPanel x:Name="descriptionSP" Background="White">
                <RichTextBlock IsTextSelectionEnabled="False" x:Name="richTextBlock" 
                               local:Properties.Html="{Binding ArticleDetail}" TextWrapping="Wrap"
                               Pivot.SlideInAnimationGroup="GroupTwo" Margin="10 5 0 10"
                                       FontFamily="{StaticResource ContentControlThemeFontFamily}">
                </RichTextBlock>
            </StackPanel>
            </StackPanel>
        </DataTemplate>
    </Page.Resources>

如何获取stackpanel中的RichTextBlock控件?

现在,我正在尝试在 C# 端使用以下代码:

private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(parentElement);
            if (count == 0) return null;

            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parentElement, i);
                if (child != null && child is T)
                    return (T)child;
                else
                {
                    var result = FindElementInVisualTree<T>(child);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }

RichTextBlock richTextBlock = new RichTextBlock();
            StackPanel rootStackPanel = new StackPanel();
            StackPanel childStackPanel = new StackPanel();

    PivotItem item = (sender as Pivot).ContainerFromItem((sender as Pivot).SelectedItem) as PivotItem;
            rootStackPanel = item.ContentTemplate.LoadContent() as StackPanel;
            childStackPanel = rootStackPanel.FindName("descriptionSP") as StackPanel;
            richTextBlock = rootStackPanel.FindName("richTextBlock") as RichTextBlock;

 Paragraph paragraph = new Paragraph();
            Run run = new Run();

            // Customize some properties on the RichTextBlock.
            richTextBlock.IsTextSelectionEnabled = true;
            richTextBlock.SelectionHighlightColor = new SolidColorBrush(Windows.UI.Colors.Pink);
            richTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Blue);
            richTextBlock.FontWeight = Windows.UI.Text.FontWeights.Light;
            richTextBlock.FontFamily = new FontFamily("Arial");
            richTextBlock.FontStyle = Windows.UI.Text.FontStyle.Italic;
            richTextBlock.FontSize = 50;
            //run.Text = "This is some sample text to demonstrate some properties.";

            //Add the Run to the Paragraph, the Paragraph to the RichTextBlock.
            paragraph.Inlines.Add(run);
            richTextBlock.Blocks.Add(paragraph);

            // Add the RichTextBlock to the visual tree (assumes stackPanel is decalred in XAML).
            //childStackPanel.Children.Add(richTextBlock);
            //rootStackPanel.Children.Add(richTextBlock);

但我无法获得控件 RichTextBlock。我得到一个 null 值。 请帮我。 谢谢。

最佳答案

您可以使用 PivotItemContentTemplate 来获取 PivotItem 的模板,例如:

PivotItem item = (sender as Pivot).ContainerFromItem((sender as Pivot).SelectedItem) as PivotItem;
var rootStackPanel = item.ContentTemplate.LoadContent() as StackPanel;
var richtb = rootStackPanel.FindName("richtb") as RichTextBlock;

我首先给RichTextBlock取了个名字“richtb”。

关于c# - 如何在 UWP 的 Pivot 中获取 Pivot.ItemTemplate 中的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38829367/

相关文章:

c# - 使 baloonTipText 可见直到它被点击

c# - DateTime 从 C# 到 MySQL : incomplete storage

c# - 通用字典上 IEqualityComparer<TKey> 的用例

c# - 如何通过用户名监控文件服务器上的文件访问和更改?

c# - UWP:显示排列成圆圈的 ListView 项目

c# - 连接到 VPN 会停止 UWP 应用程序互联网通信

c# - Xaml 内部错误错误 WMC9999

c# - 有人可以在继承Windows控件时解释此LinkDemand警告吗?

c# - 分配给 SqlXml 参数的 XML 进行了额外编码?

c# - 如何告诉 ASP.Net MVC 从 JSON 反序列化的所有传入日期都应该是 UTC?