从WPF中的RichTextBox显示LineNumbers

我找到了一个示例,如何在Windows窗体中显示RichTextBox中的LineNumbers。 http://www.codeproject.com/Articles/38858/Line-Numbers-for-RichText-Control-in-C

WPF中有人为它做一个例子吗?

编辑:

是否有人与AvalonEdit合作,因为他想在他的程序中显示LineNumbers并且可以帮助我解决问题。

我想只是添加一个简单的解决方案……

Avalon ( 下载 )已被提及,这里是如何用它做行行:

   

它是一个非常强大的编辑器 – 我在它和生产(自定义语言编译器等)中做了很多。 它是免费的,所以它有它的’怪癖’,但它允许你做你需要的任何东西。 我不确定你在谈论哪种格式化的文本 – 但是你需要的任何东西都可以完成,并且代码不多,如果你知道如何注入,你可以添加自己的发生器,变换器,几乎任何视觉效果,代码完整等等 – 我对它没有既得利益:)

编辑

用于语法突出显示的小样本:

  

支持的是"XmlDoc", "C#", "JavaScript", "HTML", "ASP/XHTML", "Boo", "Coco", "CSS", "C+", "Java", "Patch", "PHP", "TeX", "VBNET", "XML" ,据我所知,从代码中可以看出。

如果你想实现自己的自定义syntax highlighting (Avalon编辑器是高度可扩展的 – 有关详细信息,请参阅提到的代码项目文章 )…

您需要定义自己的IHighlightingDefinition ,然后从代码中使用HighlightingManager注册它 – 然后再添加一些内容来补充。 但这本身就是一个很长的故事。

只需为TextBox使用自定义模板; 以下答案可能对您有所帮助:

TextBlock的可见行数

更新


更改答案以获得OP预期的工作。

  

设计师,

     

附属物,

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; namespace Cmsn.Software.Tutorials.numberedTextBox { public class AttachedProperties { #region BindableLineCount AttachedProperty public static string GetBindableLineCount(DependencyObject obj) { return (string)obj.GetValue(BindableLineCountProperty); } public static void SetBindableLineCount(DependencyObject obj, string value) { obj.SetValue(BindableLineCountProperty, value); } // Using a DependencyProperty as the backing store for BindableLineCount. This enables animation, styling, binding, etc... public static readonly DependencyProperty BindableLineCountProperty = DependencyProperty.RegisterAttached( "BindableLineCount", typeof(string), typeof(AttachedProperties), new UIPropertyMetadata("1")); #endregion // BindableLineCount AttachedProperty #region HasBindableLineCount AttachedProperty public static bool GetHasBindableLineCount(DependencyObject obj) { return (bool)obj.GetValue(HasBindableLineCountProperty); } public static void SetHasBindableLineCount(DependencyObject obj, bool value) { obj.SetValue(HasBindableLineCountProperty, value); } // Using a DependencyProperty as the backing store for HasBindableLineCount. This enables animation, styling, binding, etc... public static readonly DependencyProperty HasBindableLineCountProperty = DependencyProperty.RegisterAttached( "HasBindableLineCount", typeof(bool), typeof(AttachedProperties), new UIPropertyMetadata( false, new PropertyChangedCallback(OnHasBindableLineCountChanged))); private static void OnHasBindableLineCountChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { var textBox = (TextBox)o; if ((e.NewValue as bool?) == true) { textBox.SizeChanged += new SizeChangedEventHandler(box_SizeChanged); textBox.SetValue(BindableLineCountProperty, textBox.LineCount.ToString()); } else { textBox.SizeChanged -= new SizeChangedEventHandler(box_SizeChanged); } } static void box_SizeChanged(object sender, SizeChangedEventArgs e) { var textBox = (TextBox)sender; string x = string.Empty; for (int i = 0; i < textBox.LineCount; i++) { x += i + 1 + "\n"; } textBox.SetValue(BindableLineCountProperty, x); } #endregion // HasBindableLineCount AttachedProperty } } 

ScintillaNET是一个很好的尝试。 它是Scintilla的.NET版本,它是Notepad ++编辑器(以及其他)中的关键组件。 我不知道它与AvalonEdit相比如何,但我发现在我的代码中实现相当容易,并且它具有您需要的function(以及更多)。

安装组件并将其添加到表单后,可以通过转到控件的属性并设置边距>边距0>宽度来显示行号 。 您也可以通过编程方式设置:

 scintilla1.Margins.Margin0.Width = 20; 

如果您计划采用这条路线,您可能会发现ScintillaNet文档很有用 – 我发现它在我开始时非常有用。

您可以找到的最佳示例是AvalonEdit 。 还有很多只是行编号,但你可以研究它是如何完成的,代码非常清晰。 关于我们所谈论的内容的一个注意点可以是CodeProject的这篇文章 。

这是一篇很好的CodeProject文章 ,它展示了在TextBox控件中实现行号和一些其他漂亮的代码编辑function的详细方法。 如果您不关心它是如何构建的,只需下载示例文件并使用包含已编译控件的包含的dll。