问题
I need to add multiple Run elements to a TextBlock. Each Run paragraph must be within the same TextBlock (to allow for selecting multiple paragraphs). However, I also need to find the height of each Run, and set the height if necessary.
Because Run does not inherit from FrameworkElement, Height and ActualHeight are not available properties. On the other hand, Run only allows children that can go in an InlineCollection (which does not include anything inheriting from FrameworkElement). This makes finding and setting the height of any child of a Run (or TextBlock, for that matter) impossible.
Is there a way to find and set the height of text elements inside a TextBlock?
As a note, I'd much rather use straight XAML/C# for its speed than have to tag on HTML/JavaScript just because XAML doesn't let one do something obvious (select multiple paragraphs with the cursor). It has also been suggested to use line heights or font size, but these don't achieve the same design effect.
回答1:
The height of Run is determined by FontSize and 1/2 LineHeight.
Although Run does not provide a Height like property to set its height, you can modify Run's height in disguise by modifying Run.FontSize or TextBlock.LineHeight.
But for text wrapping, calculating the height of a single Run is not easy. We must also consider FontFamily (because monospace fonts and non-monospace fonts behave differently under the same font size).
If an article has multiple paragraphs, you can consider using multiple TextBlocks to calculate the height of each paragraph individually.
If you want to select text across controls, you can also consider converting the text to HTML and using WebView for display.
Best regards.
来源:https://stackoverflow.com/questions/59573045/get-set-height-of-run-inside-a-paragraph-in-uwp