I'm creating a text editor for a domain specific language. I'm using the WPF RichTextBox
as the basic control. I don't know how to gracefully include line numbering. Does anyone know of any examples?
AvalonEdit is a good one, and it's open source. I think it has nearly all of the features of the Aqistar control such as syntax highlighting and folding. Ease to configure and use. Further details can be found here.
I would create a composite control, with a stack panel control and text blocks on the left which you would use to handle the line numbering. If you are concerned with the number of lines and having too many visual elements, then you could use a ListBox in virtual mode.
You would have to hook up to the various events on the RichTextBox so that you know when to update the ListBox, as well as calculate the height of each line, but that should be doable with the FlowDocument attached to the RichTextBox.
RichText supports "protected" - uneditable - spans. You could dump your line numbers as protected text spans as a part of the RTF stream (when you do your formatting).
In Win Forms, you can use RichTextBox.SelectionProtected Property. WPF must have something similar.
This way, all your baselines will be correct and you won't have to do any extra thinking/programming to get the editor to behave properly. Editable text will be editable, and line numbers will not be.
Only down side is that you have to resubmit the RTF stream after every edit. But I imagine you were already doing this to provide parse formatting / error diagnostics / whatever.
How about using AqiStar's rich text WPF control? AqiStar.TextBox
I don't work for them...I just know that Oren is using this product in his NHibernate Profiler (NHProf) and he seems to dig it.
来源:https://stackoverflow.com/questions/567404/wpf-richtextbox-to-create-editor-with-line-numbers