如何在UWP中的Pivot中获取Pivot.ItemTemplate内的控件?(How to get controls inside Pivot.ItemTemplate in Pivot in UW

系统教程 行业动态 更新时间:2024-06-14 16:52:52
如何在UWP中的Pivot中获取Pivot.ItemTemplate内的控件?(How to get controls inside Pivot.ItemTemplate in Pivot in 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#end中的以下代码:

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

I have the following code:

<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>

How to get the RichTextBlock control inside the stackpanel?

Now, I am trying with the following code in the C# end:

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

But I am not able to get the control RichTextBlock. I am getting a null value. Please help me. Thanks.

最满意答案

您可以使用PivotItem ContentTemplate来获取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”。

You can use ContentTemplate of PivotItem to get the template of PivotItem for example like this:

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;

And I firstly gave a name to the RichTextBlock as "richtb".

更多推荐

本文发布于:2023-04-05 12:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/85d12aa5da45de54e00aca3bfe5518ba.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   如何在   UWP   Pivot   ItemTemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!