WPF,richtextbox高亮文本 [英] WPF,richtextbox highlight text

查看:87
本文介绍了WPF,richtextbox高亮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  if(sorting [j]> sorting [j + 1]){交换(sorting,j,j + 1);SortingTraceInfo sortInfo = new SortingTraceInfo();//结构变量sortInfo.Position = j;//更改位置保存sortInfo.TargetPosition = j + 1;//更改位置保存sortInfo.SortingNumbers = sorting.ToArray();//sortingInfos.Add(sortInfo);} 

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

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

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

每单击一次按钮,输出一行,并且颜色发生变化.

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

index(位置= 3,tartgetposition = 4),

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

index(位置= 5,tartgetposition = 6),

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

index(位置= 6,tartgetposition = 7),

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

解决方案

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

 < StackPanel>< RichTextBox Name =输出"></RichTextBox><按钮内容=下一步"单击="Next_OnClick"/></StackPanel> 

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

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

     公共结构SortingTraceInfo{public int职位;public int TargetPosition;public int [] SortingNumbers;}public int [] Sorting = new [] {23,59,59,70,12,92,19,14,77,51};公共列表< SortingTraceInfo>SortingInfos = new List< SortingTraceInfo>();私有void Next_OnClick(对象发送者,RoutedEventArgs e){Output.Document.Blocks.Clear();for(int j = 0; j< Sorting.Count()-1; j ++){如果(排序[j]>排序[j + 1]){//渲染到实时出价为(int i = 0; i< Sorting.Count(); i ++){如果(i == j){//将数字渲染为红色var textRange = new TextRange(Output.Document.ContentEnd,Output.Document.ContentEnd);textRange.Text =排序[i] +";textRange.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Red);}否则if(i == j + 1){//将数字设为蓝色var textRange = new TextRange(Output.Document.ContentEnd,Output.Document.ContentEnd);textRange.Text = Sorting [i] +";textRange.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Blue);}别的{//将数字设为黑色var textRange = new TextRange(Output.Document.ContentEnd,Output.Document.ContentEnd);textRange.Text =排序[i] +";textRange.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Black);}}//交换(排序,j,j + 1);int tmp =排序[j];Sorting [j] = Sorting [j + 1];排序[j + 1] = tmp;var sortInfo = new SortingTraceInfo();//结构变量sortInfo.Position = j;//更改位置保存sortInfo.TargetPosition = j + 1;//更改位置保存sortInfo.SortingNumbers = Sorting.ToArray();//SortingInfos.Add(sortInfo);//一次处理一个分类操作休息;}}} 

结果:

等...

I save the information on the location of the sort algorithm changes into an array.

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);     
}

I know the index of the changed position. And it outputs the result was the richtextbox.

It applied the index(sortInfo.position) that was saved in the result(in richtextbox).

result in richtextbox. It is applied to the index. The result I want is

Output one line and color changes each time you click the button.

23 59 59 70 12 92 19 14 77 51 -> 70 < color red, 12 color blue

index ( position = 3, tartgetposition = 4),

23 59 59 12 70 92 19 14 77 51 -> 92 < color red, 19 color blue

index ( position = 5, tartgetposition = 6),

23 59 59 12 70 19 92 14 77 51 -> 92 < color red, 14 color blue

index ( position = 6, tartgetposition = 7),

However, I failed........

解决方案

If i understood you properly, your problem is how to render texts in a RichTextBox with different Colors ! To do that you could use a TextRange for each Array item and apply brush color based on the item situation (Position -> Red, TargetPosition -> Blue, others -> Black), so for the following RichTextBox:

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

you need at each Next's Button Click :

  • Clear the Output RichTetex
  • Iterate through the Sorting Array, and create a TextRang based on the item index
  • A loop break operation should be executed each time a swap operation occurred to properly visualize the output

    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;
            }               
        }           
    }
    

the result :

etc...

这篇关于WPF,richtextbox高亮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆