sch*_*her 6 c# wpf xaml textbox maxlength
我有一个TextBoxWPF.我想限制文本的长度TextBox.有一种简单的方法可以限制属性的字符数MaxLength.
在我的用例中,我需要限制文本而不是字符数,而是限制给定编码中文本的二进制表示的长度.由于德国人使用的程序有一些变音符号,它消耗两个字节.
我已经有一个方法,检查给定的字符串是否适合给定的长度:
public bool IsInLength(string text, int maxLength, Encoding encoding)
{
return encoding.GetByteCount(text) < maxLength;
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何以某种方式将此功能绑定到文本框,用户不可能输入太多字符来超过最大字节长度.
没有EventHandler的解决方案是首选,因为TextBox位于DataTemplate中.
我扩展了Alex Klaus的解决方案,以防止输入太长的文本。
public class TextBoxMaxLengthBehavior : Behavior<TextBox>
{
public static readonly DependencyProperty MaxLengthProperty =
DependencyProperty.Register(
nameof(MaxLength),
typeof(int),
typeof(TextBoxMaxLengthBehavior),
new FrameworkPropertyMetadata(0));
public int MaxLength
{
get { return (int) GetValue(MaxLengthProperty); }
set { SetValue(MaxLengthProperty, value); }
}
public static readonly DependencyProperty LengthEncodingProperty =
DependencyProperty.Register(
nameof(LengthEncoding),
typeof(Encoding),
typeof(TextBoxMaxLengthBehavior),
new FrameworkPropertyMetadata(Encoding.Default));
public Encoding LengthEncoding
{
get { return (Encoding) GetValue(LengthEncodingProperty); }
set { SetValue(LengthEncodingProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewTextInput += PreviewTextInputHandler;
DataObject.AddPastingHandler(AssociatedObject, PastingHandler);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewTextInput -= PreviewTextInputHandler;
DataObject.RemovePastingHandler(AssociatedObject, PastingHandler);
}
private void PreviewTextInputHandler(object sender, TextCompositionEventArgs e)
{
string text;
if (AssociatedObject.Text.Length < AssociatedObject.CaretIndex)
text = AssociatedObject.Text;
else
{
// Remaining text after removing selected text.
string remainingTextAfterRemoveSelection;
text = TreatSelectedText(out remainingTextAfterRemoveSelection)
? remainingTextAfterRemoveSelection.Insert(AssociatedObject.SelectionStart, e.Text)
: AssociatedObject.Text.Insert(AssociatedObject.CaretIndex, e.Text);
}
e.Handled = !ValidateText(text);
}
private bool TreatSelectedText(out string text)
{
text = null;
if (AssociatedObject.SelectionLength <= 0)
return false;
var length = AssociatedObject.Text.Length;
if (AssociatedObject.SelectionStart >= length)
return true;
if (AssociatedObject.SelectionStart + AssociatedObject.SelectionLength >= length)
AssociatedObject.SelectionLength = length - AssociatedObject.SelectionStart;
text = AssociatedObject.Text.Remove(AssociatedObject.SelectionStart, AssociatedObject.SelectionLength);
return true;
}
private void PastingHandler(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(DataFormats.Text))
{
var pastedText = Convert.ToString(e.DataObject.GetData(DataFormats.Text));
var text = ModifyTextToFit(pastedText);
if (!ValidateText(text))
e.CancelCommand();
else if (text != pastedText)
e.DataObject.SetData(DataFormats.Text, text);
}
else
e.CancelCommand();
}
private string ModifyTextToFit(string text)
{
var result = text.Remove(MaxLength);
while (!string.IsNullOrEmpty(result) && !ValidateText(result))
result = result.Remove(result.Length - 1);
return result;
}
private bool ValidateText(string text)
{
return LengthEncoding.GetByteCount(text) <= MaxLength;
}
}
Run Code Online (Sandbox Code Playgroud)
在 XAML 中我可以这样使用它:
<DataTemplate DataType="{x:Type vm:StringViewModel}">
<TextBox Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}">
<i:Interaction.Behaviors>
<b:TextBoxMaxLengthBehavior MaxLength="{Binding MaxLength}" LengthEncoding="{Binding LengthEncoding}" />
</i:Interaction.Behaviors>
</TextBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
在哪里xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"。我希望这对其他人有帮助。
| 归档时间: |
|
| 查看次数: |
579 次 |
| 最近记录: |