从WPF RichTextBox上的代码编辑特定行
如何从代码编辑RichTextBox中的特定行 我向RichTextBox添加行从WPF RichTextBox上的代码编辑特定行,wpf,richtextbox,Wpf,Richtextbox,如何从代码编辑RichTextBox中的特定行 我向RichTextBox添加行 FlowDocument mcFlowDoc = new FlowDocument(); Paragraph para = new Paragraph(); para.Inlines.Add(new Run("I am a RichTextBox control line 1\n")); para.Inlines.Add(new Run("I am a RichTextBox control line 2\n")
FlowDocument mcFlowDoc = new FlowDocument();
Paragraph para = new Paragraph();
para.Inlines.Add(new Run("I am a RichTextBox control line 1\n"));
para.Inlines.Add(new Run("I am a RichTextBox control line 2\n"));
para.Inlines.Add(new Run("I am a RichTextBox control line 3\n")
{
Foreground = Brushes.Red
});
mcFlowDoc.Blocks.Add(para);
RichTextBox1.Document = mcFlowDoc;
我的XAML
<RichTextBox Margin="5" Name="RichTextBox1" FontSize="16" VerticalAlignment="Top"
Width="500" Height="220">
</RichTextBox>
现在我想在单击按钮的同时添加一个按钮(单击事件),第二行将变为“我终于开始做了”如果您的文本结构始终相同,您只需在单击处理程序中找到所需的运行并更改它,如下所示:
var par = (Paragraph) RichTextBox1.Document.Blocks.FirstOrDefault();
var run = (Run) par.Inlines.Skip(1).First();
run.Text = "I finally got to do it\n";
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
string lineToReplace = "I am a RichTextBox control line 2";
string newLine = "I finally got to do it";
TextRange text = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(lineToReplace);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(lineToReplace.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newLine;
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
RichTextBox1.Selection.Select(selection.Start, selection.End);
RichTextBox1.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}
又快又脏:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
RichTextBox1.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
RichTextBox1.Document.ContentEnd
);
// get the lines of text
string[] lines =textRange.Text.Split(new[] { Environment.NewLine } , StringSplitOptions.RemoveEmptyEntries);
// get the second line
lines[1] = "I finally got to do it" + Environment.NewLine;
// build a string from the string array
StringBuilder builder = new StringBuilder();
foreach (string value in lines)
{
builder.Append(value);
}
// test the text in the RichTextbox
Paragraph para = new Paragraph();
para.Inlines.Add(new Run(builder.ToString()));
RichTextBox1.Document.Blocks.Clear();
RichTextBox1.Document.Blocks.Add(para);
}
并在文档中正确添加换行符:
FlowDocument mcFlowDoc = new FlowDocument();
Paragraph para = new Paragraph();
para.Inlines.Add(new Run("I am a RichTextBox control line 1"));
para.Inlines.Add(Environment.NewLine);
para.Inlines.Add(new Run("I am a RichTextBox control line 2"));
para.Inlines.Add(Environment.NewLine);
para.Inlines.Add(new Run("I am a RichTextBox control line 3")
{
Foreground = Brushes.Red
});
mcFlowDoc.Blocks.Add(para);
RichTextBox1.Document = mcFlowDoc;
替换如下所示的行:
var par = (Paragraph) RichTextBox1.Document.Blocks.FirstOrDefault();
var run = (Run) par.Inlines.Skip(1).First();
run.Text = "I finally got to do it\n";
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
string lineToReplace = "I am a RichTextBox control line 2";
string newLine = "I finally got to do it";
TextRange text = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(lineToReplace);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(lineToReplace.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newLine;
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
RichTextBox1.Selection.Select(selection.Start, selection.End);
RichTextBox1.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}
我的文本有数千行,我只需要编辑一行。为此,我需要一遍又一遍地阅读所有内容?一定有更快的。因为我必须每秒在不同的位置编辑几次,明白了。我发布了另一个答案您的代码在特定点上添加文本,我需要编辑特定行。@Danymor,您如何知道要编辑哪一行?您仍然可以使用Inlines集合获取所需的行,只需使用specific
Where()
。例如,如果您事先知道要更改的行,可以将Run.Name
设置为用于搜索。非常感谢它的帮助。是否可以仅在当前行(光标所在的行)中执行测试和更改?我认为我们必须为此改变这条路线。TextRange text=新的TextRange(RichTextBox1.Document.ContentStart,RichTextBox1.Document.ContentEnd);