Repository navigation
Description
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 exampleTextset beforeMaxLength, orMaxLengthlowered later), the user can't delete characters one by one: Backspace and Delete are refused unless the deletion brings the text withinMaxLength. - Undo can't put such text back, and Redo can't redo typing that happened before
MaxLengthwas lowered. PasswordBox.Passwordset from code pastMaxLengthis refused the same way on current master. On 6.8,Passwordkeeps the value but the box displays nothing.SelectedTextset from code is refused when the result is longer thanMaxLength.
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,
SelectedTextorPasswordis kept whole, and the change events are raised as usual. - Typing that would make the text longer than
MaxLengthis 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