Is there a way to force the text in an Avalonia TextBox to be uppercase?
This doesn't seem to work:
<Label Content="Password:"></Label>
<TextBox Name="PasswordTextBox" TextInputOptions.Uppercase="True" />
<Label Content="Password:"></Label>
<TextBox Name="PasswordTextBox" TextInputOptions.Uppercase="True" />
Putting this in the code-behind works, but it's a lot of code to write for a feature that exists on every platform I've used for years and years:
public PasswordPrompt()
{
InitializeComponent();
PasswordTextBox.TextChanged += (sender, args) =>
{
var tb = (TextBox)sender!;
if (tb.Text is { } text)
{
var selectionStart = tb.CaretIndex; // remember caret
tb.Text = text.ToUpperInvariant();
tb.CaretIndex = Math.Min(selectionStart, tb.Text.Length);
}
};
}
Thanks!
This can't be done through XAML alone at the moment. There's a related issue and a draft PR open that would allow you to do <TextBox CharacterCasing="Upper"/>, but it doesn't seem to be moving forward. The TextInputOptions class you found is unrelated and TextBox doesn't have a property with that type.
For now, you'll just have to keep doing this in your code-behind or view model.