I am trying to set the MaxLength
property on a RichTextBox
but it does not seem to work.
我试图在RichTextBox上设置MaxLength属性,但它似乎不起作用。
Any ideas what might be happening?
有什么想法可能会发生什么?
2 个解决方案
#1
The fundamental problem is that the WPF RichTextBox doesn't have a MaxLength property - unlike the Windows.Forms one.
根本问题是WPF RichTextBox没有MaxLength属性 - 与Windows.Forms属性不同。
Here's an improvement on @jhony's anwser. If you catch the PreviewKeyDown event and check the length, you also need to allow the user to press Delete and BackSpace after hitting the limit.
这是@jhony的anwser的改进。如果捕获PreviewKeyDown事件并检查长度,则还需要允许用户在达到限制后按Delete和BackSpace。
// In constructor
this.RichTextBox.PreviewKeyDown += this.EditBox_KeyDown;
private void EditBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Delete && e.Key != Key.Back)
{
var range = new TextRange(this.RichTextBox.Document.ContentStart, this.RichTextBox.Document.ContentEnd);
if (range.Text.Length > this.MaxLength)
{
e.Handled = true;
return;
}
}
}
You should also allow the arrow keys, because you wouldn't expect them to be disabled.
您还应该允许使用箭头键,因为您不希望它们被禁用。
To disable pasting, put this in the constructor DataObject.AddPastingHandler(this.RichTextBox, this.EditBox_Paste);
and
要禁用粘贴,请将其放在构造函数DataObject.AddPastingHandler(this.RichTextBox,this.EditBox_Paste)中;和
private void EditBox_Paste(object sender, DataObjectPastingEventArgs e)
{
e.CancelCommand();
}
However, you might want to allow pasting unless it breaks the MaxLength, in which case you'll need to check the text being inserted, and the text it is replacing. At that point I decided not to implement a MaxLength in the control, but to handle it with the rest of the validation in the form.
但是,您可能希望允许粘贴,除非它破坏MaxLength,在这种情况下,您需要检查正在插入的文本以及它正在替换的文本。那时我决定不在控件中实现MaxLength,而是在表单中的其余验证中处理它。
#2
Add this code in yout KeyDown Event.
在你的KeyDown事件中添加此代码。
private void rLetter_KeyDown(object sender, KeyEventArgs e)
{
TextRange tr= new TextRange(rLetter.Document.ContentStart, rLetter.Document.ContentEnd);
if (tr.Text.Length > 4000 || e.Key == Key.Space || e.Key == Key.Enter)
{
e.Handled = true;
return;
}
}
I have some problem, The bug is :
我有一些问题,错误是:
Test this code using copy and paste text max 4000.
使用复制和粘贴文本max 4000测试此代码。
Sorry for my English ..
对不起我的英语不好 ..
#1
The fundamental problem is that the WPF RichTextBox doesn't have a MaxLength property - unlike the Windows.Forms one.
根本问题是WPF RichTextBox没有MaxLength属性 - 与Windows.Forms属性不同。
Here's an improvement on @jhony's anwser. If you catch the PreviewKeyDown event and check the length, you also need to allow the user to press Delete and BackSpace after hitting the limit.
这是@jhony的anwser的改进。如果捕获PreviewKeyDown事件并检查长度,则还需要允许用户在达到限制后按Delete和BackSpace。
// In constructor
this.RichTextBox.PreviewKeyDown += this.EditBox_KeyDown;
private void EditBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Delete && e.Key != Key.Back)
{
var range = new TextRange(this.RichTextBox.Document.ContentStart, this.RichTextBox.Document.ContentEnd);
if (range.Text.Length > this.MaxLength)
{
e.Handled = true;
return;
}
}
}
You should also allow the arrow keys, because you wouldn't expect them to be disabled.
您还应该允许使用箭头键,因为您不希望它们被禁用。
To disable pasting, put this in the constructor DataObject.AddPastingHandler(this.RichTextBox, this.EditBox_Paste);
and
要禁用粘贴,请将其放在构造函数DataObject.AddPastingHandler(this.RichTextBox,this.EditBox_Paste)中;和
private void EditBox_Paste(object sender, DataObjectPastingEventArgs e)
{
e.CancelCommand();
}
However, you might want to allow pasting unless it breaks the MaxLength, in which case you'll need to check the text being inserted, and the text it is replacing. At that point I decided not to implement a MaxLength in the control, but to handle it with the rest of the validation in the form.
但是,您可能希望允许粘贴,除非它破坏MaxLength,在这种情况下,您需要检查正在插入的文本以及它正在替换的文本。那时我决定不在控件中实现MaxLength,而是在表单中的其余验证中处理它。
#2
Add this code in yout KeyDown Event.
在你的KeyDown事件中添加此代码。
private void rLetter_KeyDown(object sender, KeyEventArgs e)
{
TextRange tr= new TextRange(rLetter.Document.ContentStart, rLetter.Document.ContentEnd);
if (tr.Text.Length > 4000 || e.Key == Key.Space || e.Key == Key.Enter)
{
e.Handled = true;
return;
}
}
I have some problem, The bug is :
我有一些问题,错误是:
Test this code using copy and paste text max 4000.
使用复制和粘贴文本max 4000测试此代码。
Sorry for my English ..
对不起我的英语不好 ..