Browse Source

Use textpresenter to replace with password char.

Dan Walmsley 7 years ago
parent
commit
8befbacba0

+ 14 - 0
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<char> PasswordCharProperty =
+            AvaloniaProperty.Register<TextPresenter, char>(nameof(PasswordChar));
+
         public static readonly DirectProperty<TextPresenter, int> SelectionStartProperty =
             TextBox.SelectionStartProperty.AddOwner<TextPresenter>(
                 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;

+ 1 - 22
src/Avalonia.Controls/TextBox.cs

@@ -56,9 +56,6 @@ namespace Avalonia.Controls
                 defaultBindingMode: BindingMode.TwoWay,
                 enableDataValidation: true);
 
-        public static readonly StyledProperty<string> DisplayTextProperty =
-            AvaloniaProperty.Register<TextBox, string>(nameof(DisplayText));
-
         public static readonly StyledProperty<TextAlignment> TextAlignmentProperty =
             TextBlock.TextAlignmentProperty.AddOwner<TextBox>();
 
@@ -122,19 +119,7 @@ namespace Avalonia.Controls
                 ScrollViewer.HorizontalScrollBarVisibilityProperty,
                 horizontalScrollBarVisibility,
                 BindingPriority.Style);
-            _undoRedoHelper = new UndoRedoHelper<UndoRedoState>(this);
-
-            this.GetObservable(TextProperty).Subscribe(text =>
-            {
-                if (IsPasswordBox)
-                {
-                    DisplayText = new string(PasswordChar, text.Length);
-                }
-                else
-                {
-                    DisplayText = Text;
-                }
-            });
+            _undoRedoHelper = new UndoRedoHelper<UndoRedoState>(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); }

+ 3 - 2
src/Avalonia.Themes.Default/TextBox.xaml

@@ -38,12 +38,13 @@
                              Text="{TemplateBinding Watermark}"
                              IsVisible="{TemplateBinding Path=Text, Converter={x:Static StringConverters.NullOrEmpty}}"/>
                   <TextPresenter Name="PART_TextPresenter"
-                                 Text="{TemplateBinding DisplayText, Mode=TwoWay}"
+                                 Text="{TemplateBinding Text, Mode=TwoWay}"
                                  CaretIndex="{TemplateBinding CaretIndex}"
                                  SelectionStart="{TemplateBinding SelectionStart}"
                                  SelectionEnd="{TemplateBinding SelectionEnd}"
                                  TextAlignment="{TemplateBinding TextAlignment}"
-                                 TextWrapping="{TemplateBinding TextWrapping}"/>
+                                 TextWrapping="{TemplateBinding TextWrapping}"
+                                 PasswordChar="{TemplateBinding PasswordChar}"/>
                 </Panel>
               </ScrollViewer>
             </DataValidationErrors>