Show LineNumbers from the RichTextBox in WPF

Karl_Schuhmann picture Karl_Schuhmann · Mar 25, 2013 · Viewed 10.6k times · Source

I found an example, how to show the LineNumbers from a RichTextBox in Windows Forms. http://www.codeproject.com/Articles/38858/Line-Numbers-for-RichText-Control-in-C

Have somebody an example for it in WPF?

Edit:

Does someone have work with AvalonEdit, because he wanted to show LineNumbers in his Programm and can help me by my Problem.

Answer

NSGaga-mostly-inactive picture NSGaga-mostly-inactive · Apr 8, 2013

I thought just to add a simple solution...

Avalon (Download) has been mentioned, here is how to do row lines with it:

<avalon:TextEditor ShowLineNumbers="True" Width="500" Height="500"
                    Text="some more test&#10; some more test&#10; some more test&#10; some more test&#10; some more test&#10; some more test&#10; some more test&#10; some more test&#10; some more test&#10; ">
</avalon:TextEditor>  

It's a very powerful editor - and I've been doing a lot with it and in production (custom language compilers etc.). It's free so it has its 'quirks' but it's allowing you to do pretty much anything you need. I'm not sure which formatted text are you talking about - but anything you need can be done, and in not too many lines of code, if you know how, where to inject (you can add your own generators, transformers, pretty much any visuals, code completition etc. - and I have no vested interest in it:)

Edit

Small sample for syntax highlighting:

<avalon:TextEditor 
  ShowLineNumbers="True" 
  SyntaxHighlighting="C#" Width="500" Height="500"
  Text="void Test(int id, string name)&#10;{&#10;&#09;name = name + id.ToString();}" />  

Supported is "XmlDoc", "C#", "JavaScript", "HTML", "ASP/XHTML", "Boo", "Coco", "CSS", "C+", "Java", "Patch", "PHP", "TeX", "VBNET", "XML" as far as I can tell from the code.

If you'd like to implement your own custom syntax highlighting (Avalon editor is highly extendable - for details see that code project article mentioned)...

You'd need to define your own IHighlightingDefinition, then register that with the HighlightingManager from the code - and then add some more things to complement. But that's a long story on its own.