From 8befbacba018f8d36bb271d1d6a4c5ea8203c5c1 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 9 May 2018 19:41:53 +0100 Subject: [PATCH] Use textpresenter to replace with password char. --- .../Presenters/TextPresenter.cs | 14 +++++++++++ src/Avalonia.Controls/TextBox.cs | 23 +------------------ src/Avalonia.Themes.Default/TextBox.xaml | 5 ++-- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index d2d4151e3d..902f951146 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -17,6 +17,9 @@ namespace Avalonia.Controls.Presenters o => o.CaretIndex, (o, v) => o.CaretIndex = v); + public static readonly StyledProperty PasswordCharProperty = + AvaloniaProperty.Register(nameof(PasswordChar)); + public static readonly DirectProperty SelectionStartProperty = TextBox.SelectionStartProperty.AddOwner( o => o.SelectionStart, @@ -63,6 +66,12 @@ namespace Avalonia.Controls.Presenters } } + public char PasswordChar + { + get => GetValue(PasswordCharProperty); + set => SetValue(PasswordCharProperty, value); + } + public int SelectionStart { get @@ -204,6 +213,11 @@ namespace Avalonia.Controls.Presenters protected override FormattedText CreateFormattedText(Size constraint) { + if (PasswordChar != default(char)) + { + Text = new string(PasswordChar, Text.Length); + } + var result = base.CreateFormattedText(constraint); var selectionStart = SelectionStart; var selectionEnd = SelectionEnd; diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index fb3eef742a..874d2a3229 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -56,9 +56,6 @@ namespace Avalonia.Controls defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true); - public static readonly StyledProperty DisplayTextProperty = - AvaloniaProperty.Register(nameof(DisplayText)); - public static readonly StyledProperty TextAlignmentProperty = TextBlock.TextAlignmentProperty.AddOwner(); @@ -122,19 +119,7 @@ namespace Avalonia.Controls ScrollViewer.HorizontalScrollBarVisibilityProperty, horizontalScrollBarVisibility, BindingPriority.Style); - _undoRedoHelper = new UndoRedoHelper(this); - - this.GetObservable(TextProperty).Subscribe(text => - { - if (IsPasswordBox) - { - DisplayText = new string(PasswordChar, text.Length); - } - else - { - DisplayText = Text; - } - }); + _undoRedoHelper = new UndoRedoHelper(this); } public bool AcceptsReturn @@ -232,12 +217,6 @@ namespace Avalonia.Controls } } - public string DisplayText - { - get => GetValue(DisplayTextProperty); - set => SetValue(DisplayTextProperty, value); - } - public TextAlignment TextAlignment { get { return GetValue(TextAlignmentProperty); } diff --git a/src/Avalonia.Themes.Default/TextBox.xaml b/src/Avalonia.Themes.Default/TextBox.xaml index 89c445eac5..c57e5b792d 100644 --- a/src/Avalonia.Themes.Default/TextBox.xaml +++ b/src/Avalonia.Themes.Default/TextBox.xaml @@ -38,12 +38,13 @@ Text="{TemplateBinding Watermark}" IsVisible="{TemplateBinding Path=Text, Converter={x:Static StringConverters.NullOrEmpty}}"/> + TextWrapping="{TemplateBinding TextWrapping}" + PasswordChar="{TemplateBinding PasswordChar}"/>