2017-12-20 15 views
1

昨日私の質問に答えた後、私はこのコードを書いたが、「段落」はRichTextBlockOverflowの「子」ではないが最初のRichTextBlock(これは理由)。だから、どのようなRichTextBlockOverflowが「段落リスト」の段落であるかを知ることができるかどうかを知りたいと思います。たとえば、数字2のようになります。段落が存在するRichTextBlockOverflowを見つける

生成されるエラーは次のとおりです。System.NullReferenceException: 'オブジェクト参照が設定されていませんオブジェクトのインスタンスに追加します。 btnParentボタン(例2)のクリックで生成されます。

XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center"> 
     <Grid x:Name="Menù" Width="290" Padding="0" Margin="40"> 
      <Grid Background="White"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="50"/> 
        <RowDefinition Height="50"/> 
        <RowDefinition Height="50"/> 
       </Grid.RowDefinitions> 
       <Button Grid.Row="0" x:Name="btnLoadText" Click="btnLoadText_Click" Content="Display text" HorizontalAlignment="Center" VerticalAlignment="Center" Width="270" Foreground="White" Height="32"/> 
       <Button Grid.Row="1" x:Name="btnParent" Click="btnParent_Click" Content="Parent Name" HorizontalAlignment="Center" VerticalAlignment="Center" Width="270" Foreground="White" Height="32"/> 
       <Button Grid.Row="2" x:Name="btnCount" Click="btnCount_Click" Content="Count" HorizontalAlignment="Center" VerticalAlignment="Center" Width="270" Foreground="White" Height="32"/> 
      </Grid> 
     </Grid> 
     <Grid x:Name="BaseGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="720" Height="350"> 
      <ScrollViewer x:Name="PageViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Visible" VerticalScrollMode="Disabled" HorizontalScrollMode="Enabled" Background="LightGreen"> 
       <StackPanel x:Name="StackViewer" Orientation="Horizontal"/> 
      </ScrollViewer> 
     </Grid> 
    </StackPanel> 
</Grid> 

コード:RichTextBlockOverflow段落番号2つの存在の名前を見つけるため

public sealed partial class MainPage : Page 
{ 
    List<Paragraph> paragraphlist = new List<Paragraph>(); 
    RichTextBlock TextOneRich = new RichTextBlock() { Margin = new Thickness(20) }; 
    List<RichTextBlockOverflow> TextList = new List<RichTextBlockOverflow>(); 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     StackViewer.Children.Add(TextOneRich); 
     TextOneRich.Width = 200; 
     TextOneRich.TextAlignment = TextAlignment.Justify; 
     TextOneRich.Name = "RichTextOne"; 
    } 

    private async void btnLoadText_Click(object sender, RoutedEventArgs e) 
    { 
     TextList.Clear(); 
     TextOneRich.Blocks.Clear(); 
     StackViewer.Children.Clear(); 
     StackViewer.Children.Add(TextOneRich); 

     Paragraph paragraphONE = new Paragraph(); 
     paragraphONE.Inlines.Clear(); 
     paragraphONE.Inlines.Add(new Run { Text = "ONE Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus." }); 
     paragraphlist.Add(paragraphONE); 

     Paragraph paragraphTWO = new Paragraph(); 
     paragraphTWO.Inlines.Clear(); 
     paragraphTWO.Inlines.Add(new Run { Text = "TWO Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus." }); 
     paragraphlist.Add(paragraphTWO); 

     Paragraph paragraphTHREE = new Paragraph(); 
     paragraphTHREE.Inlines.Clear(); 
     paragraphTHREE.Inlines.Add(new Run { Text = "THREE Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus." }); 
     paragraphlist.Add(paragraphTHREE); 

     int ThereIsTexts = 0; 
     int CountName = 1; 
     for (int i = 0; i <= ThereIsTexts; i++) 
     { 
      if (i == 0) 
      { 
       TextOneRich.Blocks.Add(paragraphONE); 
       TextOneRich.Blocks.Add(paragraphTWO); 
       TextOneRich.Blocks.Add(paragraphTHREE); 

       await Task.Delay(300); 

       if (TextOneRich.HasOverflowContent) 
       { 
        TextList.Add(new RichTextBlockOverflow() { Width = 200, Margin = new Thickness(20) }); 
        StackViewer.Children.Add(TextList[TextList.Count - 1]); 
        TextOneRich.OverflowContentTarget = TextList[TextList.Count - 1]; 
        TextList[TextList.Count - 1].Name = "RichOverflo" + CountName.ToString(); 
        CountName += 1; 
        ThereIsTexts += 1; 
       } 
      } 
      else 
      { 
       await Task.Delay(300); 
       if (TextList[TextList.Count - 1].HasOverflowContent) 
       { 
        TextList.Add(new RichTextBlockOverflow() { Width = 200, Margin = new Thickness(20) }); 
        StackViewer.Children.Add(TextList[TextList.Count - 1]); 
        TextList[TextList.Count - 2].OverflowContentTarget = TextList[TextList.Count - 1]; 
        TextList[TextList.Count - 1].Name = "RichOverflo" + CountName.ToString(); 
        CountName += 1; 
        ThereIsTexts += 1; 
       } 
      } 
     } 
    } 

    private async void btnParent_Click(object sender, RoutedEventArgs e) 
    { 
     //// *** Exmple 1 work *** 
     //RichTextBlock parent = (paragraphlist[2] as Paragraph).ElementStart.Parent as RichTextBlock; 
     //string parentName = (parent as RichTextBlock).Name; 
     //var messageDialog = new MessageDialog("The parent is: " + parentName); 
     //await messageDialog.ShowAsync(); 


     //// *** Example 2 not work *** 
     RichTextBlockOverflow parent = (paragraphlist[2] as Paragraph).ElementStart.Parent as RichTextBlockOverflow; 
     string parentName = (parent as RichTextBlockOverflow).Name; 
     var messageDialog = new MessageDialog("The parent is: " + parentName); 
     await messageDialog.ShowAsync(); 
    } 

    private async void btnCount_Click(object sender, RoutedEventArgs e) 
    { 
     var messageDialog = new MessageDialog("The number of Bloks is: " + TextOneRich.Blocks.Count); 
     await messageDialog.ShowAsync(); 
    } 
} 

ことは可能ですか?

ありがとうございます!これはあなたのケースで動作するはず

答えて

2

:一般的に

RichTextBlockOverflow parent = paragraphlist[2].ElementStart.VisualParent as RichTextBlockOverflow; 

を、段落が複数RichTextBlockOverflow要素とRichTextBlockを占めることができます。あなたがそれらのすべてを見つける必要がある場合は、最初のものと最後のものは次のようになります。それらの間の

var firstOccupiedBlock = paragraph.ElementStart.VisualParent; 
var lastOccupiedBlock = paragraph.ElementEnd.VisualParent; 

そしてRichTextBlockOverflow要素がfirstOccupiedBlockから始まるチェーンRichTextBlockOverflow要素のOverflowContentTarget財産の道を辿ることによって見つけることができます。

+0

細かい動作ありがとう! – Tibrus

+0

@ ibrusあなたは大歓迎です! –

関連する問題