jeudi 8 janvier 2015

How to Iteratively highlight text parts in Paragraph on button click


Vote count:

0




I have following FlowDocument with Paragraph in my XAML file:



<FlowDocumentScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
<FlowDocument Name="fDocument" PagePadding="10" FontFamily="Segoe UI" FontSize="22">
<Paragraph Name="fdParagraph">
Those who have denied the reality of moral distinctions, may be
ranked among the disingenuous disputants; nor is it conceivable,
that any human creature could ever seriously believe, that all
characters and actions were alike entitled to the affection and
regard of everyone. The difference, which nature has placed
between one man and another, is so wide, and this difference is
still so much farther widened, by education, example, and habit,
that, where the opposite extremes come at once under our
apprehension, there is no scepticism so scrupulous, and scarce
any assurance so determined, as absolutely to deny all
distinction between them. Let a man's insensibility be ever so
great, he must often be touched with the images of Right and
Wrong; and let his prejudices be ever so obstinate, he must
observe, that others are susceptible of like impressions. The
only way, therefore, of converting an antagonist of this kind, is
to leave him to himself.
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>


Basically, what I want to achieve is to, on each click, how to highlight next text part in this paragraph?


Therefore, I have one idea so far on how to achieve this. However, I'm not sure if it is efficient and possible to do or is to complicated, since I don't have any experience with FlowDocument.


So my idea was to, according to example I found a moment ago MSDN:Insert an Element Into Text Programmatically, was to create a new class, for example let's call it ParagraphController. ParagraphController class would extend Paragraph class. So, according to this example,



// Create a paragraph with a short sentence
Paragraph myParagraph = new Paragraph(new Run("Neptune has 72 times Earth's volume..."));

// Create two TextPointers that will specify the text range the Span will cover
TextPointer myTextPointer1 = myParagraph.ContentStart.GetPositionAtOffset(10);
TextPointer myTextPointer2 = myParagraph.ContentEnd.GetPositionAtOffset(-5);

// Create a Span that covers the range between the two TextPointers.
Span mySpan = new Span(myTextPointer1, myTextPointer2);
mySpan.Background = Brushes.Red;


I would define following in my class:



  • paragraphPartsIndexes - 2d array which would contain beginIndex and endIndex for each paragraph text part

  • list of paragraph text parts (which comes from paragraph splited by space)

  • int currentPartIndex - for iteration through a list of text parts


So now I guess, I could have something like a following method below, which I can invoke in my Button Click method, in order to highlight next text part each time i click, so the logic would be something like this ?



void method() {
foreach (part in textParts) {
begin = myParagraph.ContentStart.GetPositionAtOffset(paragraphPartsIndexes[currentPartIndex][0]);
end = myParagraph.ContentEnd.GetPositionAtOffset(paragraphPartsIndexes[currentPartIndex][1]);

// condition, just to make sure it is a correct text part to highlight
if (part.equals(myParagraph.Content(begin, end))) {
Span mySpan = new Span(myTextPointer1, myTextPointer2); // part to highlight
mySpan.Background = Brushes.Red; // highlight color
currentPartIndex++; // prepare next paragraph part to be highlighted
}
}
}


Now, suggestions or your answers with a better solution on my first question - how to highlight next text part in this paragraph?



asked 1 min ago







How to Iteratively highlight text parts in Paragraph on button click

Aucun commentaire:

Enregistrer un commentaire