【问题标题】:How to get controls inside Pivot.ItemTemplate in Pivot in UWP?如何在 UWP 中的 Pivot 中获取 Pivot.ItemTemplate 中的控件?
【发布时间】:2016-12-14 05:43:00
【问题描述】:

我有以下代码:

 <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 值。 请帮我。 谢谢。

【问题讨论】:

    标签: c# .net uwp


    【解决方案1】:

    您可以使用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”。

    【讨论】:

    • 感谢@grace,我能够获得richtextblock 控件。我有问题。我正在尝试在 c# 结束时将字体大小设置为richtextblock 作为richtb.Fontsize=20。但这并不影响。但如果我从 xaml 设置它工作正常。在后端设置字体大小是正确的方法还是有其他方法可以设置?请帮我。谢谢
    • @BhanuprakashMankala,对于RichTextBlock,我们需要在后面的代码中访问这个控件的Block,内容以块为单位,可以参考RichTextBlock class中的示例。
    • 在我的 XAML 中,我正在绑定来自“local:Properties.Html”的数据,并且我没有使用段落属性。是否可以使用我的代码获取块?
    • @BhanuprakashMankala,有可能,你可以试试。
    • 嗨@grace,我已经编辑了我的问题。请调查一下。我正在尝试获取块,尝试自定义richtextblock。但是,它不起作用。这是正确的方法吗?请帮帮我。
    【解决方案2】:

    您提供的代码在树中搜索给定类型的第一个匹配控件。这意味着,您得到的回报是 XAML 定义中的第一个 StackPanel,但第二个中的 RichTextBlock

    你为什么不直接这样做,而不是搜索StackPanel

    var richTextBlock = FindElementInVisualTree<RichTextBlock>(item);
    

    这将直接返回RichTextBlock,因为FindElementInVisualTree 方法正在递归搜索树。

    【讨论】:

    • 我也试过了,在这种情况下我也得到了空值。
    • 你能检查item变量是否不为空吗?
    • 我已经尝试过 Grace 的上述回答,并且我能够获得richtextblock 控件。
    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2017-12-21
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多