Skip to content

TextBox ignores a Text value set from code when it is longer than MaxLength #25997

Open

Description

@mikernet

Current behavior 🐛

When a TextBox has MaxLength set, setting Text from code (directly, through a Binding or x:Bind, or inside a TextChanging or SelectionChanged handler while the user types) to a value longer than MaxLength does nothing: TextChanging, BeforeTextChanging and TextChanged aren't raised, and the TextBox keeps showing its previous text (with a binding, the previous item's text).

This can lose data. In an app that reuses one TextBox for a list of items, moving to an item whose value is longer than MaxLength leaves the previous item's text on screen. The user's next edit then writes that text, edited, into the current item, replacing its real value.

Related symptoms:

  • When the text is already longer than MaxLength (for example Text set before MaxLength, or MaxLength lowered later), the user can't delete characters one by one: Backspace and Delete are refused unless the deletion brings the text within MaxLength.
  • Undo can't put such text back, and Redo can't redo typing that happened before MaxLength was lowered.
  • PasswordBox.Password set from code past MaxLength is refused the same way on current master. On 6.8, Password keeps the value but the box displays nothing.
  • SelectedText set from code is refused when the result is longer than MaxLength.

Expected behavior 🎯

As on WinUI, MaxLength limits only user input (docs: "the maximum number of characters allowed for user input"):

  • Text set from code, through bindings, SelectedText or Password is kept whole, and the change events are raised as usual.
  • Typing that would make the text longer than MaxLength is refused, and raises no events.
  • Deleting works even while the text is still longer than MaxLength.
  • Undo and Redo put back the text they recorded.

These results were checked against a WinUI 3 app (Windows App SDK 1.8) with MaxLength = 5:

Action WinUI result
Text = "0123456789" before or after loading 0123456789
TwoWay Binding to "LongValue123" LongValue123
Text = "0123456789", then type x at the end 0123456789 (refused, no events)
then Backspace 012345678
then select 3 characters at the start and type y 012345678 (refused, selection kept)
Text = "0123456789", select 6 characters at the start, type v v6789
Backspace, then Undo() 0123456789
PasswordBox.Password = "0123456789" 0123456789
A TextChanging handler sets Text = "0123456789" while the user types 0123456789

How to reproduce it (as minimally and precisely as possible) 🔬

<TextBox x:Name="tb" MaxLength="5" />
tb.Text = "0123456789";
// Expected: "0123456789". Actual: "" (the previous value).
Debug.WriteLine(tb.Text);

With a binding:

public sealed class Item : INotifyPropertyChanged
{
	private string _value = "";
	public string Value
	{
		get => _value;
		set { _value = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value))); }
	}
	public event PropertyChangedEventHandler? PropertyChanged;
}

var tb = new TextBox { MaxLength = 5, DataContext = new Item { Value = "abc" } };
tb.SetBinding(TextBox.TextProperty, new Binding
{
	Path = new("Value"),
	Mode = BindingMode.TwoWay,
	UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
});
// ... once loaded:
var other = new Item { Value = "0123456789" };
tb.DataContext = other; // tb still shows "abc"
// Put the caret at the end of the TextBox and press Backspace: other.Value becomes "ab"

Workaround 🛠️

Clear MaxLength (set it to 0) before setting Text from code, then set it back. There is no simple workaround for bindings.

Affected platforms 📱💻🖥️

WebAssembly, Android, iOS, tvOS, Desktop (Windows), Desktop (macOS), Desktop (X11), Desktop (Linux Framebuffer)

Uno.Sdk version (and other relevant versions) 📦

Uno.Sdk 6.8.0-dev.66 (Uno.WinUI 6.8.0-dev.222), and current master at 58d7b41. The rule dates from 2019 (#1353).

IDE version 🧑‍💻

N/A

Anything else we need to know? 💬

The value is refused by the Text coercion, which applies MaxLength to every change, whether it comes from user input or from code. A fix is ready.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions