Kar*_*ann 8 c# wpf richtextbox line-numbers
我找到了一个例子,如何RichTextBox在Windows窗体中显示LineNumbers .
http://www.codeproject.com/Articles/38858/Line-Numbers-for-RichText-Control-in-C
在WPF中有人为它做一个例子吗?
编辑:
是否有人与AvalonEdit合作,因为他想在他的程序中显示LineNumbers并且可以通过我的问题帮助我.
我想只是添加一个简单的解决方案......
Avalon(下载)已被提及,这里是如何用它做行行:
<avalon:TextEditor ShowLineNumbers="True" Width="500" Height="500"
Text="some more test some more test some more test some more test some more test some more test some more test some more test some more test ">
</avalon:TextEditor>
Run Code Online (Sandbox Code Playgroud)
它是一个非常强大的编辑器 - 我在它和生产(自定义语言编译器等)中做了很多.它是免费的,所以它有它的'怪癖',但它允许你做你需要的任何东西.我不确定你在谈论哪种格式的文本 - 但是你需要的任何东西都可以完成,而且代码行数不多,如果你知道如何注入,你可以添加自己的发生器,变换器,几乎任何视觉效果,代码完整性等等 - 我对它没有任何既得利益:)
用于语法突出显示的小样本:
<avalon:TextEditor
ShowLineNumbers="True"
SyntaxHighlighting="C#" Width="500" Height="500"
Text="void Test(int id, string name) { 	name = name + id.ToString();}" />
Run Code Online (Sandbox Code Playgroud)
"XmlDoc", "C#", "JavaScript", "HTML", "ASP/XHTML", "Boo", "Coco", "CSS", "C+", "Java", "Patch", "PHP", "TeX", "VBNET", "XML"从代码中我可以看出支持.
如果你想实现自己的自定义syntax highlighting(Avalon编辑器是高度可扩展的 - 有关详细信息,请参阅提到的代码项目文章)...
你需要定义自己的IHighlightingDefinition,然后用HighlightingManager代码注册它- 然后添加一些东西来补充.但这本身就是一个很长的故事.
只需为TextBox使用自定义模板; 以下答案可能对您有所帮助:
更新
更改答案以获得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>
Run Code Online (Sandbox Code Playgroud)
设计师,
<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>
Run Code Online (Sandbox Code Playgroud)
附属物,
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
}
}
Run Code Online (Sandbox Code Playgroud)