From 00a4c0c5ec95f41f5864567108cb4baa7b6eacbe Mon Sep 17 00:00:00 2001 From: textGamex Date: Wed, 5 Jun 2024 04:16:55 +0800 Subject: [PATCH] fix: TextBox, NumberBox, PasswordBox Placeholder Not Working Properly. (#1109) --- src/Wpf.Ui/Controls/NumberBox/NumberBox.xaml | 18 +++---- .../Controls/PasswordBox/PasswordBox.cs | 12 +---- .../Controls/PasswordBox/PasswordBox.xaml | 10 ++-- src/Wpf.Ui/Controls/TextBox/TextBox.cs | 47 +++++++++++++++---- src/Wpf.Ui/Controls/TextBox/TextBox.xaml | 2 +- 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/Wpf.Ui/Controls/NumberBox/NumberBox.xaml b/src/Wpf.Ui/Controls/NumberBox/NumberBox.xaml index 361a9bb5b..58f36f9a1 100644 --- a/src/Wpf.Ui/Controls/NumberBox/NumberBox.xaml +++ b/src/Wpf.Ui/Controls/NumberBox/NumberBox.xaml @@ -54,10 +54,10 @@ + Foreground="{TemplateBinding Foreground}" + IsTabStop="False" /> + Foreground="{DynamicResource TextControlButtonForeground}" + IsTabStop="False"> @@ -178,8 +178,8 @@ VerticalAlignment="Top" Content="{TemplateBinding Icon}" FontSize="16" - IsTabStop="False" - Foreground="{TemplateBinding Foreground}" /> + Foreground="{TemplateBinding Foreground}" + IsTabStop="False" /> @@ -192,7 +192,7 @@ CornerRadius="{TemplateBinding Border.CornerRadius}" /> - + diff --git a/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.cs b/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.cs index d49a0d341..e4be9da26 100644 --- a/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.cs +++ b/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.cs @@ -88,7 +88,7 @@ public bool IsPasswordRevealed } /// - /// Gets or sets a value indicating whether whether to display the password reveal button. + /// Gets or sets a value indicating whether to display the password reveal button. /// public bool RevealButtonEnabled { @@ -125,15 +125,7 @@ protected override void OnTextChanged(TextChangedEventArgs e) } else { - if (PlaceholderEnabled && Text.Length > 0) - { - SetCurrentValue(PlaceholderEnabledProperty, false); - } - - if (!PlaceholderEnabled && Text.Length < 1) - { - SetCurrentValue(PlaceholderEnabledProperty, true); - } + SetPlaceholderTextVisibility(); RevealClearButton(); } diff --git a/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.xaml b/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.xaml index 871698bba..346b8a68d 100644 --- a/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.xaml +++ b/src/Wpf.Ui/Controls/PasswordBox/PasswordBox.xaml @@ -204,8 +204,8 @@ Command="{Binding Path=TemplateButtonCommand, RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="clear" Cursor="Arrow" - IsTabStop="False" - Foreground="{DynamicResource TextControlButtonForeground}"> + Foreground="{DynamicResource TextControlButtonForeground}" + IsTabStop="False"> @@ -228,8 +228,8 @@ Command="{Binding Path=TemplateButtonCommand, RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="reveal" Cursor="Arrow" - IsTabStop="False" - Foreground="{DynamicResource TextControlButtonForeground}"> + Foreground="{DynamicResource TextControlButtonForeground}" + IsTabStop="False"> @@ -256,7 +256,7 @@ CornerRadius="{TemplateBinding Border.CornerRadius}" /> - + diff --git a/src/Wpf.Ui/Controls/TextBox/TextBox.cs b/src/Wpf.Ui/Controls/TextBox/TextBox.cs index e33781008..befd9169f 100644 --- a/src/Wpf.Ui/Controls/TextBox/TextBox.cs +++ b/src/Wpf.Ui/Controls/TextBox/TextBox.cs @@ -48,6 +48,13 @@ public class TextBox : System.Windows.Controls.TextBox new PropertyMetadata(true) ); + /// Identifies the dependency property. + public static readonly DependencyProperty CurrentPlaceholderEnabledProperty = DependencyProperty.Register( + nameof(CurrentPlaceholderEnabled), + typeof(bool), + typeof(TextBox), + new PropertyMetadata(true)); + /// Identifies the dependency property. public static readonly DependencyProperty ClearButtonEnabledProperty = DependencyProperty.Register( nameof(ClearButtonEnabled), @@ -99,7 +106,7 @@ public ElementPlacement IconPlacement } /// - /// Gets or sets numbers pattern. + /// Gets or sets placeholder text. /// public string PlaceholderText { @@ -108,7 +115,7 @@ public string PlaceholderText } /// - /// Gets or sets a value indicating whether to display the placeholder text. + /// Gets or sets a value indicating whether to enable the placeholder text. /// public bool PlaceholderEnabled { @@ -116,6 +123,15 @@ public bool PlaceholderEnabled set => SetValue(PlaceholderEnabledProperty, value); } + /// + /// Gets or sets a value indicating whether to display the placeholder text. + /// + public bool CurrentPlaceholderEnabled + { + get => (bool)GetValue(CurrentPlaceholderEnabledProperty); + protected set => SetValue(CurrentPlaceholderEnabledProperty, value); + } + /// /// Gets or sets a value indicating whether to enable the clear button. /// @@ -154,6 +170,7 @@ public bool IsTextSelectionEnabled public TextBox() { SetValue(TemplateButtonCommandProperty, new RelayCommand(OnTemplateButtonClick)); + CurrentPlaceholderEnabled = PlaceholderEnabled; } /// @@ -161,17 +178,29 @@ protected override void OnTextChanged(TextChangedEventArgs e) { base.OnTextChanged(e); - if (PlaceholderEnabled && Text.Length > 0) + SetPlaceholderTextVisibility(); + + RevealClearButton(); + } + + protected void SetPlaceholderTextVisibility() + { + if (PlaceholderEnabled) { - SetCurrentValue(PlaceholderEnabledProperty, false); + if (CurrentPlaceholderEnabled && Text.Length > 0) + { + SetCurrentValue(CurrentPlaceholderEnabledProperty, false); + } + + if (!CurrentPlaceholderEnabled && Text.Length < 1) + { + SetCurrentValue(CurrentPlaceholderEnabledProperty, true); + } } - - if (!PlaceholderEnabled && Text.Length < 1) + else { - SetCurrentValue(PlaceholderEnabledProperty, true); + SetCurrentValue(CurrentPlaceholderEnabledProperty, false); } - - RevealClearButton(); } /// diff --git a/src/Wpf.Ui/Controls/TextBox/TextBox.xaml b/src/Wpf.Ui/Controls/TextBox/TextBox.xaml index 8e197b9c0..4fc1e5940 100644 --- a/src/Wpf.Ui/Controls/TextBox/TextBox.xaml +++ b/src/Wpf.Ui/Controls/TextBox/TextBox.xaml @@ -214,7 +214,7 @@ CornerRadius="{TemplateBinding Border.CornerRadius}" /> - +