-->

从RichTextBox的WPF中显示LineNumbers(Show LineNumbers fr

2019-07-23 05:23发布

我发现了一个例子,如何从显示LineNumbers RichTextBox Windows窗体。 http://www.codeproject.com/Articles/38858/Line-Numbers-for-RichText-Control-in-C

可有人在WPF它的例子吗?

编辑:

是否有人有工作,AvalonEdit,因为他想显示他的PROGRAMM LineNumbers,可以通过我的问题,帮助我。

Answer 1:

我以为只是增加一个简单的解决方案...

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>  

这是一个非常强大的编辑器 - 我已经做了很多与它在生产(自定义语言编译器等)。 它是免费的,所以它有它的“怪癖”,但它让你做几乎任何你需要的。 我不知道这是格式化你说的文本 - 但你们需要什么可以做,并没有太多的代码行,如果你知道如何,到哪里注入(你可以添加自己的发电机,变压器,几乎任何的视觉效果,代码completition等等 - 我在它没有既得利益:)

编辑

语法高亮小样本:

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

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

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

你需要定义自己的IHighlightingDefinition ,然后注册,与HighlightingManager从代码-然后添加一些更多的东西来补充。 但是,这本身就是一个很长的故事。



Answer 2:

只需使用你的文本框自定义模板; 以下的答案可以帮助你:

TextBlock的可视行数

更新


更改答案让作品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>

设计师,

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

附加属性,

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


Answer 3:

ScintillaNET是一个很好的组件尝试。 这是Scintilla的的.NET版本是在一个关键组成部分记事本++编辑器(等等)。 我不知道如何比较AvalonEdit,但我发现它很容易在我的代码实现,它有你需要的功能(还有更多)。

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

scintilla1.Margins.Margin0.Width = 20;

您可能会发现ScintillaNet文档 ,如果你打算走这条路有用的-我发现它真的有用,当我开始。



Answer 4:

最好你能找到的例子是AvalonEdit 。 还有很多呢,只是行号,但你可以研究它是怎么做的,代码是很清楚的。 一个起始点,看看我们所谈论的可能是这个CodeProject上的文章 。



Answer 5:

这里是一个不错的CodeProject上的文章 ,显示了详细的方法来实施行号及其他不错的代码编辑功能TextBox控件屈指可数。 如果你不关心它是如何建立,只需下载示例文件,并使用附带的DLL包含编译控制。



文章来源: Show LineNumbers from the RichTextBox in WPF