C# 在WPF中显示RichTextBox中的行号
我找到了一个例子,如何在Windows窗体中显示C# 在WPF中显示RichTextBox中的行号,c#,wpf,richtextbox,line-numbers,C#,Wpf,Richtextbox,Line Numbers,我找到了一个例子,如何在Windows窗体中显示RichTextBox中的行号。 有人在WPF中为它举个例子吗 编辑: 是否有人使用过AvalonEdit,因为他想在程序中显示行号,可以帮助我解决问题。只需为您的文本框使用自定义模板;以下答案可能对您有所帮助: 更新 更改答案以获得预期效果 <Style x:Key="Local_TextBox" TargetType="{x:Type TextBoxBase}"> <Setter Property="Templa
RichTextBox
中的行号。
有人在WPF中为它举个例子吗
编辑:
是否有人使用过AvalonEdit,因为他想在程序中显示行号,可以帮助我解决问题。只需为您的文本框使用自定义模板;以下答案可能对您有所帮助: 更新
更改答案以获得预期效果
<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
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
命名空间Cmsn.Software.Tutorials.numberedTextBox
{
公共类附件属性
{
#region BindableLineCount AttachedProperty
公共静态字符串GetBindableLineCount(DependencyObject obj)
{
返回(字符串)obj.GetValue(BindableLineCountProperty);
}
公共静态void SetBindableLineCount(DependencyObject对象,字符串值)
{
对象设置值(BindableLineCountProperty,值);
}
//使用DependencyProperty作为BindableLineCount的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty BindableLineCountProperty=
DependencyProperty.RegisterAttached(
“BindableLineCount”,
类型(字符串),
类型(附件属性),
新UIPropertyMetadata(“1”);
#endregion//BindableLineCount附件属性
#区域HasBindableLineCount AttachedProperty
公共静态bool GetHasBindableLineCount(DependencyObject obj)
{
返回(bool)对象GetValue(HasBindableLineCountProperty);
}
公共静态void SetHasBindableLineCount(DependencyObject对象,布尔值)
{
obj.SetValue(HasBindableLineCountProperty,value);
}
//使用DependencyProperty作为HasBindableLineCount的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty HasBindableLineCountProperty=
DependencyProperty.RegisterAttached(
“HasBindableLineCount”,
类型(bool),
类型(附件属性),
新UIPropertyMetadata(
假,,
新属性ChangedCallback(OnHasBindableLineCountChanged));
HasBindableLineCountChanged上的私有静态无效(DependencyObject o、DependencyPropertyChangedEventArgs e)
{
var textBox=(textBox)o;
if((例如,NewValue作为bool?==true)
{
textBox.SizeChanged+=新的sizechangedventhandler(box\u SizeChanged);
SetValue(BindableLineCountProperty,textBox.LineCount.ToString());
}
其他的
{
textBox.SizeChanged-=新的sizechangedventhandler(box\u SizeChanged);
}
}
静态空盒\u SizeChanged(对象发送方,SizeChangedEventArgs e)
{
var textBox=(textBox)发送方;
string x=string.Empty;
对于(int i=0;i
最好的。不管怎样,仅仅是行编号还有很多,但您可以研究它是如何完成的,代码非常清楚。可以从一个起点来了解我们正在谈论的内容。是一个值得尝试的组件。它是闪烁体的.NET版本,是编辑器中的一个关键组件(以及其他组件)。我不知道它与AvalonEdit相比如何,但我发现在我的代码中实现它相当容易,并且它具有您需要的功能(以及更多功能)
完成并将其添加到表单后,可以通过转到控件的属性并设置边距>边距0>宽度来完成。您还可以通过编程方式设置:
scintilla1.Margins.Margin0.Width = 20;
如果你打算走这条路,你可能会发现它很有用——我刚开始的时候发现它非常有用。我想添加一个简单的解决方案 已经提到了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>
)
您需要定义自己的IHighlightingDefinition
,然后从代码中向HighlightingManager
注册,然后添加一些补充内容。但这本身就是一个很长的故事。这里有一个nice,它展示了在文本框控件中实现行号和一些其他nice代码编辑功能的详细方法。如果您不关心它是如何构建的,只需下载示例文件并使用包含已编译控件的附带dll。您是否考虑过问题中提到的任何选项?Aqistar似乎不再是一个替代品(链接已失效),但可能是一个可行的替代品?您想为此使用MVVM还是不使用MVVM?您可能需要创建自己的自定义文本框和其他东西来实现这一点。@Faisal Hafeez不使用MVVM.-1因为OP wan不只是计算行数,但是在RichEdit上显示行号。谢谢您的回答。它可以工作,但我不能使用您的解决方案,因为我必须在richTextBoxHope下载一个带有格式化文本的FlowDocument。投票人看到@Karl_Schuhmann的评论说它可以工作。将侦听器更改为textBox.TextChanged+=new textchangedventhadler(box_SizeChanged)代码>我知道AvaloneEdit,但我必须在我的richtextbox中加载格式化文本。我不能用高亮显示。你能给我看看吗
<avalon:TextEditor
ShowLineNumbers="True"
SyntaxHighlighting="C#" Width="500" Height="500"
Text="void Test(int id, string name) { 	name = name + id.ToString();}" />