我想用TextHighlighter对象突出显示富文本块的一些文本。我创建了一个TextRange,并将其添加到一个列表中,然后创建了一个新的TextHighlighter实例并设置了背景色。但是现在我不能用TextHighlighter来突出显示文本。我该怎么做?
xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
<RichTextBlock x:Name="RichFullText" Margin="0,0,0,10">
<Paragraph x:Name="Testo">
<Run Foreground="Blue" FontSize="24" FontStyle="Italic">
This is a
</Run>
<Run Foreground="Teal" FontFamily="Segoe UI Light" FontSize="18" >
example text
</Run>
<Run Foreground="Black" FontFamily="Arial" FontSize="14" FontWeight="Bold">
format
</Run>
</Paragraph>
</RichTextBlock>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
<TextBox x:Name="txbToFind" Height="32" VerticalAlignment="Bottom" Width="200" HorizontalAlignment="Left"/>
<Button x:Name="btnToFind" Content="Find" Click="btnToFind_Click" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</StackPanel>
</Grid>xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void btnToFind_Click(object sender, RoutedEventArgs e)
{
TextRange textRange = new TextRange() { StartIndex = 3, Length = 5 };
List<TextRange> rangelist = new List<TextRange>();
rangelist.Add(textRange);
TextHighlighter evidenziatore = new TextHighlighter() { Background = new SolidColorBrush(Colors.Yellow) };
//RichFullText... There I would apply highlight to RichTextBlock
}
}如何使用TextHighlighter突出显示
发布于 2018-01-28 21:17:38
您需要将TextHighlighter添加到RichTextBlock的TextHighlighters集合中:
TextRange textRange = new TextRange() { StartIndex = 3, Length = 10 };
TextHighlighter highlighter = new TextHighlighter()
{
Background = new SolidColorBrush(Colors.Yellow),
Ranges = { textRange }
};
//add the highlighter
RichBlock.TextHighlighters.Add(highlighter);还请注意,我已经将textRange添加到TextHighlighter实例中的Ranges集合中。
https://stackoverflow.com/questions/48477203
复制相似问题