C# 突出显示文本richtextblock UWP
我想突出显示文本,同时保持相同的格式:粗体、下划线、斜体。。。但我只是通过丢失格式来突出显示文本。达到我想要的是正确的吗?或者有没有其他方法可以突出显示文本,而不必“拆分”文本,然后重新组装 xaml:C# 突出显示文本richtextblock UWP,c#,uwp,highlight,C#,Uwp,Highlight,我想突出显示文本,同时保持相同的格式:粗体、下划线、斜体。。。但我只是通过丢失格式来突出显示文本。达到我想要的是正确的吗?或者有没有其他方法可以突出显示文本,而不必“拆分”文本,然后重新组装 xaml: 这是一个 不同文本 格式 xaml.cs: public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } pri
这是一个
不同文本
格式
xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void btnFind_Click(object sender, RoutedEventArgs e)
{
string text = string.Empty;
TextBlock NewText = new TextBlock();
string toFind = txbToFind.Text;
for (int a = 0; a <= Testo.Inlines.Count - 1; a++)
{
Run runCorrente = Testo.Inlines[a] as Run;
string currentText;
currentText = runCorrente.Text;
text += currentText;
}
if (text.IndexOf(toFind) >= 0)
{
string[] partOfText = text.Split(new String[] { toFind }, StringSplitOptions.None);
Paragraph paragraph = new Paragraph();
for (int a = 0; a <= partOfText.Length - 1; a++)
{
var piece = partOfText[a];
Run run = new Run();
run.Text = piece;
paragraph.Inlines.Add(run);
if (a < partOfText.Length - 1)
{
MakeHighlightedParagraph(paragraph, toFind, RichTextBlockText);
}
}
RichTextBlockText.Blocks.Clear();
RichTextBlockText.Blocks.Add(paragraph);
}
}
private void MakeHighlightedParagraph(Paragraph paragraph, string textToHighlight, RichTextBlock textBlock)
{
InlineUIContainer cont = new InlineUIContainer();
var border = new Border();
border.MinWidth = textBlock.FontSize / 3.5;
border.Background = new SolidColorBrush(Colors.Yellow);
var text = new TextBlock();
text.Text = textToHighlight;
var margin = textBlock.FontSize * (3.0 / 14.0) + 1.0;
text.Margin = new Thickness(0.0, 0.0, 0.0, -margin);
border.Child = text;
cont.Child = border;
paragraph.Inlines.Add(cont);
}
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有void btnFind\u单击(对象发送者,路由目标)
{
string text=string.Empty;
TextBlock NewText=新TextBlock();
字符串toFind=txbToFind.Text;
对于(int a=0;a=0)
{
string[]partOfText=text.Split(新字符串[]{toFind},StringSplitOptions.None);
段落=新段落();
对于(int a=0;a在RichTextBlock
控件中没有一个简单的属性或方法可以满足您在RichTextBlock
中查找文本而不丢失格式的要求,您应该在RichTextBlock
中的每个文本上设置属性,使它们重新具有格式。作为一个简单的scenario,您也可以通过Run
对象过滤文本,然后配置所有文本的格式。在RichTextBlock
控件中没有一个简单的属性或方法可以满足您在RichTextBlock
中查找文本而不丢失格式的要求,您应该在eve上设置属性在RichTextBlock
中过滤文本,使其重新具有格式。作为一个简单的场景,您也可以通过Run
对象过滤文本,然后配置所有文本的格式。要将TextHighlighter
与RichTextBlock
一起使用,您可以首先创建所需的TextRange
s,初始化TextHighlighter
s使用它们,然后将它们应用到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);
要将TextHighlighter
s与RichTextBlock
一起使用,您可以首先创建所需的TextRange
s,使用它们初始化TextHighlighter
s,然后使用TextHighlighters
属性将它们应用于RichTextBlock
:
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);