Browse Source

Use textpresenter to replace with password char.

pull/1544/head
Dan Walmsley 9 years ago
parent
commit
8befbacba0
  1. 14
      src/Avalonia.Controls/Presenters/TextPresenter.cs
  2. 23
      src/Avalonia.Controls/TextBox.cs
  3. 5
      src/Avalonia.Themes.Default/TextBox.xaml

14
src/Avalonia.Controls/Presenters/TextPresenter.cs

@ -17,6 +17,9 @@ namespace Avalonia.Controls.Presenters
o => o.CaretIndex, o => o.CaretIndex,
(o, v) => o.CaretIndex = v); (o, v) => o.CaretIndex = v);
public static readonly StyledProperty<char> PasswordCharProperty =
AvaloniaProperty.Register<TextPresenter, char>(nameof(PasswordChar));
public static readonly DirectProperty<TextPresenter, int> SelectionStartProperty = public static readonly DirectProperty<TextPresenter, int> SelectionStartProperty =
TextBox.SelectionStartProperty.AddOwner<TextPresenter>( TextBox.SelectionStartProperty.AddOwner<TextPresenter>(
o => o.SelectionStart, o => o.SelectionStart,
@ -63,6 +66,12 @@ namespace Avalonia.Controls.Presenters
} }
} }
public char PasswordChar
{
get => GetValue(PasswordCharProperty);
set => SetValue(PasswordCharProperty, value);
}
public int SelectionStart public int SelectionStart
{ {
get get
@ -204,6 +213,11 @@ namespace Avalonia.Controls.Presenters
protected override FormattedText CreateFormattedText(Size constraint) protected override FormattedText CreateFormattedText(Size constraint)
{ {
if (PasswordChar != default(char))
{
Text = new string(PasswordChar, Text.Length);
}
var result = base.CreateFormattedText(constraint); var result = base.CreateFormattedText(constraint);
var selectionStart = SelectionStart; var selectionStart = SelectionStart;
var selectionEnd = SelectionEnd; var selectionEnd = SelectionEnd;

23
src/Avalonia.Controls/TextBox.cs

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

5
src/Avalonia.Themes.Default/TextBox.xaml

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

Loading…
Cancel
Save