Skip to content

fix: TextBox, NumberBox, PasswordBox Placeholder Not Working Properly. #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Wpf.Ui/Controls/NumberBox/NumberBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
<Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<Border
x:Name="ContentBorder"
MinWidth="{TemplateBinding MinWidth}"
MinHeight="{TemplateBinding MinHeight}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
MinWidth="{TemplateBinding MinWidth}"
MinHeight="{TemplateBinding MinHeight}"
Padding="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Expand All @@ -82,8 +82,8 @@
VerticalAlignment="Top"
Content="{TemplateBinding Icon}"
FontSize="16"
IsTabStop="False"
Foreground="{TemplateBinding Foreground}" />
Foreground="{TemplateBinding Foreground}"
IsTabStop="False" />
<Grid Grid.Column="1" Margin="{TemplateBinding Padding}">
<controls:PassiveScrollViewer
x:Name="PART_ContentHost"
Expand Down Expand Up @@ -115,8 +115,8 @@
Command="{Binding Path=TemplateButtonCommand, RelativeSource={RelativeSource TemplatedParent}}"
CommandParameter="clear"
Cursor="Arrow"
IsTabStop="False"
Foreground="{DynamicResource TextControlButtonForeground}">
Foreground="{DynamicResource TextControlButtonForeground}"
IsTabStop="False">
<controls:Button.Icon>
<controls:SymbolIcon FontSize="{StaticResource NumberBoxButtonIconSize}" Symbol="Dismiss24" />
</controls:Button.Icon>
Expand Down Expand Up @@ -178,8 +178,8 @@
VerticalAlignment="Top"
Content="{TemplateBinding Icon}"
FontSize="16"
IsTabStop="False"
Foreground="{TemplateBinding Foreground}" />
Foreground="{TemplateBinding Foreground}"
IsTabStop="False" />
</Grid>
</Border>
<!-- The Accent Border is a separate element so that changes to the border thickness do not affect the position of the element -->
Expand All @@ -192,7 +192,7 @@
CornerRadius="{TemplateBinding Border.CornerRadius}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="PlaceholderEnabled" Value="False">
<Trigger Property="CurrentPlaceholderEnabled" Value="False">
<Setter TargetName="PlaceholderTextBox" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="ShowClearButton" Value="False">
Expand Down
12 changes: 2 additions & 10 deletions src/Wpf.Ui/Controls/PasswordBox/PasswordBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public bool IsPasswordRevealed
}

/// <summary>
/// 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.
/// </summary>
public bool RevealButtonEnabled
{
Expand Down Expand Up @@ -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();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Wpf.Ui/Controls/PasswordBox/PasswordBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<controls:Button.Icon>
<controls:SymbolIcon FontSize="{StaticResource PasswordBoxButtonIconSize}" Symbol="Dismiss24" />
</controls:Button.Icon>
Expand All @@ -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">
<controls:Button.Icon>
<controls:SymbolIcon FontSize="{StaticResource PasswordBoxButtonIconSize}" Symbol="Eye24" />
</controls:Button.Icon>
Expand All @@ -256,7 +256,7 @@
CornerRadius="{TemplateBinding Border.CornerRadius}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="PlaceholderEnabled" Value="False">
<Trigger Property="CurrentPlaceholderEnabled" Value="False">
<Setter TargetName="PlaceholderTextBox" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="ShowClearButton" Value="False">
Expand Down
47 changes: 38 additions & 9 deletions src/Wpf.Ui/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public class TextBox : System.Windows.Controls.TextBox
new PropertyMetadata(true)
);

/// <summary>Identifies the <see cref="CurrentPlaceholderEnabled"/> dependency property.</summary>
public static readonly DependencyProperty CurrentPlaceholderEnabledProperty = DependencyProperty.Register(
nameof(CurrentPlaceholderEnabled),
typeof(bool),
typeof(TextBox),
new PropertyMetadata(true));

/// <summary>Identifies the <see cref="ClearButtonEnabled"/> dependency property.</summary>
public static readonly DependencyProperty ClearButtonEnabledProperty = DependencyProperty.Register(
nameof(ClearButtonEnabled),
Expand Down Expand Up @@ -99,7 +106,7 @@ public ElementPlacement IconPlacement
}

/// <summary>
/// Gets or sets numbers pattern.
/// Gets or sets placeholder text.
/// </summary>
public string PlaceholderText
{
Expand All @@ -108,14 +115,23 @@ public string PlaceholderText
}

/// <summary>
/// Gets or sets a value indicating whether to display the placeholder text.
/// Gets or sets a value indicating whether to enable the placeholder text.
/// </summary>
public bool PlaceholderEnabled
{
get => (bool)GetValue(PlaceholderEnabledProperty);
set => SetValue(PlaceholderEnabledProperty, value);
}

/// <summary>
/// Gets or sets a value indicating whether to display the placeholder text.
/// </summary>
public bool CurrentPlaceholderEnabled
{
get => (bool)GetValue(CurrentPlaceholderEnabledProperty);
protected set => SetValue(CurrentPlaceholderEnabledProperty, value);
}

/// <summary>
/// Gets or sets a value indicating whether to enable the clear button.
/// </summary>
Expand Down Expand Up @@ -154,24 +170,37 @@ public bool IsTextSelectionEnabled
public TextBox()
{
SetValue(TemplateButtonCommandProperty, new RelayCommand<string>(OnTemplateButtonClick));
CurrentPlaceholderEnabled = PlaceholderEnabled;
}

/// <inheritdoc />
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();
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion src/Wpf.Ui/Controls/TextBox/TextBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
CornerRadius="{TemplateBinding Border.CornerRadius}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="PlaceholderEnabled" Value="False">
<Trigger Property="CurrentPlaceholderEnabled" Value="False">
<Setter TargetName="PlaceholderTextBox" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="ShowClearButton" Value="False">
Expand Down
Loading