2016-08-08 7 views
3

私は、次のコードを持っている:StackPanelの内側RichTextBlock制御を取得する方法UWPでPivotのPivot.ItemTemplateの内部でコントロールを取得する方法は?

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

を?

今、私は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の値を得ています。 私を助けてください。おかげさまで

答えて

2

あなたはこのように、たとえばPivotItemのテンプレートを取得するためにPivotItemContentTemplateを使用することができます。

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; 

そして、私は、まず「richtb」としてRichTextBlockに名前を与えました。

+0

thanks @ grace、私はrichtextblockコントロールを取得することができます。質問があります。 richtb.Fontsize = 20のようにrichtextblockにフォントサイズを設定しようとしています。しかし、それは影響していません。しかし、私がxamlから設定していれば、うまくいきます。バックエンドでフォントサイズを設定するのは正しい方法ですか、それとも他に設定する方法がありますか?私を助けてください。ありがとうございます –

+0

@BhanuprakashMankala、 'RichTextBlock'のために、私たちはコードの背後にあるこのコントロールの' Block'にアクセスする必要があります。内容はブロックであり、あなたは[RichTextBlock class](https:// msdn .microsoft.com/ja-us/library/windows/apps/windows.ui.xaml.controls.richtextblock.aspx)。 –

+0

私のXAMLのように、私は "local:Properties.Html"からデータをバインドしています。私はParagraphプロパティを使用していません。私のコードを使ってブロックを取得することは可能ですか? –

0

指定したコードは、指定されたタイプの最初に一致するコントロールをツリーから検索します。つまり、XAMLの定義では最初のStackPanelが返されますが、2番目の定義ではRichTextBlockとなります。

あなたが直接代わり StackPanelを検索する、これをしないのはなぜ

var richTextBlock = FindElementInVisualTree<RichTextBlock>(item); 

FindElementInVisualTree方法は、再帰的にツリーを下に検索されるので、これは、直接RichTextBlockを返します。

+0

私もそれを試して、その場合にもnull値を得ています。 –

+0

'item'変数がnullでないことを確認できますか? –

+0

私はグレースによって上記の答えを試して、私はrichtextblockコントロールを取得することができます。 –

関連する問題