本文介绍了从RichTextBox的WPF中显示LineNumbers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现了一个例子,如何显示从的RichTextBox
的LineNumbers Windows窗体。
I found an example, how to show the LineNumbers from a RichTextBox
in Windows Forms.http://www.codeproject.com/Articles/38858/Line-Numbers-for-RichText-Control-in-C
可有人为它的一个示例中的 WPF ?
Have somebody an example for it in WPF?
编辑:
是否有人有工作,AvalonEdit,因为他想表现LineNumbers 。在他的PROGRAMM,可以通过我的问题,帮助我
Does someone have work with AvalonEdit, because he wanted to show LineNumbers in his Programm and can help me by my Problem.
推荐答案
只需使用你的文本框自定义模板;以下的答案可以帮助你:
Just use a custom template for your TextBox; the following answer may help you:
的
更新
更改答案让作品OP预期
Changing the answer to get works as OP expected.
<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
}
}
这篇关于从RichTextBox的WPF中显示LineNumbers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!