在 WPF 中显示 RichTextBox 中的 LineNumbers

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15610940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:17:52  来源:igfitidea点击:

Show LineNumbers from the RichTextBox in WPF

c#wpfrichtextboxline-numbers

提问by Karl_Schuhmann

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

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

Have somebody an example for it in WPF?

有人在WPF 中有一个例子吗?

Edit:

编辑:

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

有人使用 AvalonEdit 吗,因为他想在他的程序中显示 LineNumbers 并且可以帮助我解决我的问题。

回答by NSGaga-mostly-inactive

I thought just to add a simple solution...

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

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

Avalon(下载) 已经提到,这里是如何用它做行线:

<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.

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

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

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

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

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

回答by Chamika Sandamal

Just use a custom template for your TextBox; the following answer may help you:

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

Visible line count of a TextBlock

TextBlock 的可见行数

Update

更新



Changing the answer to get works as OP expected.

更改答案以按 OP 预期工作。

<Style x:Key="Local_TextBox" TargetType="{x:Type TextBoxBase}">
? ? <Setter Property="Template">
? ? ? ? <Setter.Value>
? ? ? ? ? ? <ControlTemplate TargetType="{x:Type TextBoxBase}">
? ? ? ? ? ? ? ? <Border Name="Border" Background="{TemplateBinding Background}">
? ? ? ? ? ? ? ? ? ? <ScrollViewer x:Name="PART_ContentHost" />
? ? ? ? ? ? ? ? </Border>
? ? ? ? ? ? </ControlTemplate>
? ? ? ? </Setter.Value>
? ? </Setter>
</Style>

Designer,

设计师,

<DockPanel>
    <TextBlock Text="{Binding ElementName=uiTextBox, Path=(local:AttachedProperties.BindableLineCount)}"/>
    <TextBox x:Name="uiTextBox" TextWrapping="Wrap" local:AttachedProperties.HasBindableLineCount="True"
            AcceptsReturn="True" Text="{Binding LongText}" Style="{StaticResource Local_TextBox}" />
</DockPanel>

Attached property,

附属财产,

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

回答by Ben Catterall

ScintillaNETis a good component to try. It's the .NET version of Scintilla which is a key component in the Notepad++editor (among others). I don't know how it compares to AvalonEdit, but I found it fairly easy to implement in my code and it has the feature you require (and many more).

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

Once you've installed the componentand added it to your form, you can show line numbersby going to the properties of the control and setting Margins>Margin0>Width. You can also set this programmatically:

一旦你已经安装的组件,并将其添加到您的表单,您可以显示行号通过访问控制和设置页边距> Margin0>宽度的属性。您还可以以编程方式设置:

scintilla1.Margins.Margin0.Width = 20;

You might find the ScintillaNet documentationuseful if you plan to take this route - I found it really helpful when I started.

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

回答by Felice Pollano

The best example you can find is in AvalonEdit. There is lot more anyway that just line numbering, but you can study how it is done, code is very clear. A staring point to see what we are talking about can be this CodeProject article.

你能找到的最好的例子是 AvalonEdit。反正还有很多只是行编号,但你可以研究它是如何完成的,代码很清楚。了解我们正在谈论的内容的起点可以是这篇 CodeProject 文章

回答by HotN

Here's a nice CodeProject articlethat shows a detailed approach to implementing line numbers and a handful of other nice code editing features in a TextBox control. If you don't care about how it's built, just download the sample file and use the included dll that contains the compiled control.

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