It appears that when TextBox
or ComboBox
in RowDetails cannot paste text from clipboard (when MaxLength
is reached for example) they stop precessing pasting events which are relayed to row itself. Ard so row inserts pasted text.
Solution that we implemented is to replace TextBoxes with custom ones that have only this additional code:
protected override void OnKeyDown(KeyEventArgs e)
{
if (IsPastingAndClipboardTextIsTooLarge(e.Key))
{
int textToFit = (MaxLength - Text.Length + SelectionLength);
if (textToFit > 0)
{
var startIndex = SelectionStart;
var textToPaste = Clipboard.GetText().Substring(0, Math.Min(textToFit, Clipboard.GetText().Length));
int caretPosition = startIndex + textToPaste.Length;
if (SelectionLength > 0)
Text = Text.Remove(startIndex, SelectionLength);
Text = Text.Insert(startIndex, textToPaste);
SelectionStart = caretPosition;
}
e.Handled = true;
}
else base.OnKeyDown(e);
}
public bool IsPastingAndClipboardTextIsTooLarge(Key key)
{
return key == Key.V && ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) &&
Clipboard.ContainsText() &&
Text.Length + Clipboard.GetText().Length > MaxLength - SelectionLength;
}
But ComboBoxes
will require some code too.
If there`s a better solution please let me know!