繁体   English   中英

WPF,richtextbox高亮文本

[英]WPF,richtextbox highlight text

我将有关排序算法更改的位置的信息保存到数组中。

if (sorting[j] > sorting[j + 1])
{
      Swap(sorting, j, j + 1);

      SortingTraceInfo sortInfo = new SortingTraceInfo(); // struct Variable
      sortInfo.Position = j;  // change position save
      sortInfo.TargetPosition = j + 1;    // changed position save
      sortInfo.SortingNumbers = sorting.ToArray();    // 
      sortingInfos.Add(sortInfo);     
}

我知道更改位置的索引。 它输出的结果是richtextbox。

它应用了保存在结果(在richtextbox中)中的索引(sortInfo.position)。

结果为richtextbox。 它应用于索引。 我想要的结果是

每次单击按钮,输出一行和颜色更改。

23 59 59 70 12 92 19 14 77 51-> 70 <红色,12蓝色

索引(位置= 3,tartgetposition = 4),

23 59 59 12 70 92 19 14 77 51-> 92 <红色,19蓝色

索引(位置= 5,位置tartgetposition = 6),

23 59 59 12 70 19 92 14 77 51-> 92 <红色,14蓝色

索引(位置= 6,位置tartgetposition = 7),

但是,我失败了........

如果我正确理解您的问题,那么您的问题是如何在RichTextBox以不同的颜色呈现文本! 为此,您可以为每个Array项目使用TextRange并根据项目情况(位置->红色,TargetPosition->蓝色,其他->黑色)应用画笔颜色,因此对于以下RichTextBox

<StackPanel>
    <RichTextBox Name="Output">
    </RichTextBox>
    <Button Content="Next" Click="Next_OnClick"/>
</StackPanel>

您需要在每个“下一步”按钮上单击:

  • 清除输出RichTetex
  • 遍历Sorting Array ,并根据项目索引创建TextRang
  • 每次发生交换操作时都应执行一次循环中断操作,以正确地可视化输出

     public struct SortingTraceInfo { public int Position; public int TargetPosition; public int[] SortingNumbers; } public int[] Sorting = new[] { 23, 59, 59, 70, 12, 92, 19, 14, 77, 51 }; public List<SortingTraceInfo> SortingInfos=new List<SortingTraceInfo>(); private void Next_OnClick(object sender, RoutedEventArgs e) { Output.Document.Blocks.Clear(); for (int j = 0; j < Sorting.Count()-1; j++) { if (Sorting[j] > Sorting[j + 1]) { //render to RTB for (int i = 0; i < Sorting.Count(); i++) { if (i==j) { //render the number red var textRange = new TextRange(Output.Document.ContentEnd, Output.Document.ContentEnd); textRange.Text = Sorting[i] + " "; textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red); } else if(i==j+1) { //render the number blue var textRange = new TextRange(Output.Document.ContentEnd, Output.Document.ContentEnd); textRange.Text = Sorting[i]+ " "; textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); } else { //render the number black var textRange = new TextRange(Output.Document.ContentEnd, Output.Document.ContentEnd); textRange.Text = Sorting[i] + " "; textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black); } } //Swap(Sorting, j, j + 1); int tmp=Sorting[j]; Sorting[j] = Sorting[j+1]; Sorting[j + 1] = tmp; var sortInfo = new SortingTraceInfo(); // struct Variable sortInfo.Position = j; // change position save sortInfo.TargetPosition = j + 1; // changed position save sortInfo.SortingNumbers = Sorting.ToArray(); // SortingInfos.Add(sortInfo); //handle one sorting operation one at a time break; } } } 

结果 :

在此处输入图片说明

在此处输入图片说明

等等...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM