2017-05-05 3 views
0

Je suis face à un challange en utilisant le contrôle RichTextBox:Comment récupérer du contenu RichTextBox avec les contrôles qu'il contient (boutons)?

Je suis avec succès en mesure d'ajouter des paragraphes à et boutons dans le temps de conception très bien, voir XAML ci-dessous:

<RichTextBox x:Name="rtxtStep" HorizontalAlignment="Left" Height="207" Margin="10,32,0,0" VerticalAlignment="Top" Width="427" IsDocumentEnabled="True" KeyUp="richTextBox_KeyUp"> 
      <FlowDocument> 
       <Section FontSize="15"> 
        <Paragraph> 
         Click on this: 

          <Hyperlin k NavigateUri="http://stackoverflow.com">stackoverflow</Hyperlin k> 


        </Paragraph> 

        <Paragraph> 
         <Button Click="Button_Click" Width="143" >Also Click On This</Button> 
         <Button Click="Button_Click" Width="143" >button 2</Button> 
        </Paragraph> 
       </Section> 
      </FlowDocument> 
     </RichTextBox> 

et je suis en mesure de récupérer les texte de mon code très bien, voir ci-dessous:

private void richTextBox_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Enter) 
     { 
      TextRange txtrContent = new TextRange(rtxtStep.Document.ContentStart, rtxtStep.Document.ContentEnd); 
      string allContent = txtrContent.Text; 
     } 
    } 

retour:

"Click on this: stackoverflow\r\n \r\n\r\n" 

la question est, comment puis-je récupérer les boutons ainsi que le texte?

Répondre

0

Je pense que vous vous attendez vraiment trop. Le FlowDocument dans le RichTextBox est une hiérarchie d'éléments. Le mieux que vous puissiez faire est de descendre l'arbre des éléments pour trouver les boutons. Quelque chose comme ça ...

private void richTextBox_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Enter) 
     { 
      TextRange txtrContent = new TextRange(rtxtStep.Document.ContentStart, rtxtStep.Document.ContentEnd); 
      string allContent = txtrContent.Text; 
      PrintBlocks(rtxtStep.Document.Blocks); 
     } 
    } 

    private void PrintBlocks(IEnumerable<Block> blocks) 
    { 
     foreach(Block b in blocks) 
     { 
      Trace.WriteLine("Found " + b.GetType().Name); 
      if(b is Section) 
      { 
       PrintBlocks((b as Section).Blocks); 
      } 
      else if(b is Paragraph) 
      { 
       PrintInlines((b as Paragraph).Inlines); 
      } 
     } 

    } 

    private void PrintInlines(IEnumerable<Inline> inlines) 
    { 
     foreach(Inline i in inlines) 
     { 
      if(i is InlineUIContainer) 
      { 
       PrintInlineUIContainer(i as InlineUIContainer); 
      } 

     } 
    } 
    private void PrintInlineUIContainer(InlineUIContainer i) 
    { 
     Trace.WriteLine("Found " + i.Child.GetType().Name + " " + i.Child.ToString()); 
    } 

Pour votre XAML, cette sortie est générée ...

Found Section 
Found Paragraph 
Found Paragraph 
Found Button System.Windows.Controls.Button: Also Click On This 
Found Button System.Windows.Controls.Button: button 2 

Mais vous saviez déjà. Vous semblez vouloir le texte et les boutons dans un certain format intégré (peut-être HTML?). Je pense que vous auriez besoin d'écrire votre propre code pour le faire.