Skip to content

Commit 17ff32d

Browse files
authored
Fixes the layout and resource usage for PasswordBox (#10768)
* Fixed PasswordBox style * Fixes minor issues in the new PasswordBox style
1 parent 51bb6f4 commit 17ff32d

File tree

4 files changed

+124
-167
lines changed

4 files changed

+124
-167
lines changed

src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Styles/PasswordBox.xaml

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
1414
xmlns:sys="clr-namespace:System;assembly=mscorlib">
1515

16-
<Thickness x:Key="PasswordBoxBorderThemeThickness">1,1,1,1</Thickness>
16+
<!-- Deprecated PasswordBox Resources ( Used in .NET 9 ) -->
1717
<Thickness x:Key="PasswordBoxAccentBorderThemeThickness">0,0,0,1</Thickness>
1818
<Thickness x:Key="PasswordBoxLeftIconMargin">10,8,0,0</Thickness>
1919
<Thickness x:Key="PasswordBoxRightIconMargin">0,8,10,0</Thickness>
@@ -22,30 +22,39 @@
2222
<sys:Double x:Key="PasswordBoxButtonHeight">24</sys:Double>
2323
<sys:Double x:Key="PasswordBoxButtonIconSize">14</sys:Double>
2424

25+
<!-- These rsources are redefined in .NET 10 -->
26+
<!-- <Thickness x:Key="PasswordBoxBorderThemeThickness">1,1,1,1</Thickness> -->
27+
28+
<Thickness x:Key="PasswordBoxBorderThemeThickness">1</Thickness>
29+
<!-- Instead of PasswordBoxThemeMinHeight, we need to use TextControlThemeMinHeight, but modifying that right now
30+
will affect the other styles as well. Once the remaining styles using it are fixed, will remove this resource -->
31+
<sys:Double x:Key="PasswordBoxThemeMinHeight">32</sys:Double>
32+
2533
<ContextMenu x:Key="DefaultPasswordBoxContextMenu">
2634
<MenuItem Command="ApplicationCommands.Paste" />
2735
</ContextMenu>
2836

2937
<Style x:Key="DefaultPasswordBoxStyle" TargetType="{x:Type PasswordBox}">
30-
<!-- Universal WPF UI focus -->
3138
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
32-
<!-- Universal WPF UI ContextMenu -->
3339
<Setter Property="ContextMenu" Value="{DynamicResource DefaultPasswordBoxContextMenu}" />
3440
<Setter Property="Foreground" Value="{DynamicResource TextControlForeground}" />
3541
<Setter Property="CaretBrush" Value="{DynamicResource TextControlForeground}" />
3642
<Setter Property="Background" Value="{DynamicResource TextControlBackground}" />
37-
<Setter Property="BorderBrush" Value="{DynamicResource TextControlElevationBorderBrush}" />
43+
<Setter Property="BorderBrush" Value="{DynamicResource TextControlBorderBrush}" />
3844
<Setter Property="BorderThickness" Value="{StaticResource PasswordBoxBorderThemeThickness}" />
39-
<Setter Property="HorizontalAlignment" Value="Stretch" />
40-
<Setter Property="VerticalAlignment" Value="Center" />
41-
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
45+
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
46+
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
47+
<Setter Property="HorizontalContentAlignment" Value="Left" />
4248
<Setter Property="VerticalContentAlignment" Value="Top" />
43-
<Setter Property="MinHeight" Value="{DynamicResource TextControlThemeMinHeight}" />
49+
<Setter Property="Cursor" Value="IBeam" />
50+
<Setter Property="MinHeight" Value="{DynamicResource PasswordBoxThemeMinHeight}" />
4451
<Setter Property="MinWidth" Value="{DynamicResource TextControlThemeMinWidth}" />
4552
<Setter Property="Padding" Value="{DynamicResource TextControlThemePadding}" />
46-
<Setter Property="PasswordChar" Value=""/>
4753
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
48-
<Setter Property="SnapsToDevicePixels" Value="True" />
54+
<Setter Property="AllowDrop" Value="True"/>
55+
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
56+
<Setter Property="PasswordChar" Value=""/>
57+
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
4958
<Setter Property="OverridesDefaultStyle" Value="True" />
5059
<Setter Property="Template">
5160
<Setter.Value>
@@ -55,60 +64,38 @@
5564
x:Name="ContentBorder"
5665
MinWidth="{TemplateBinding MinWidth}"
5766
MinHeight="{TemplateBinding MinHeight}"
58-
Padding="0"
59-
HorizontalAlignment="Stretch"
60-
VerticalAlignment="Stretch"
6167
Background="{TemplateBinding Background}"
6268
BorderBrush="{TemplateBinding BorderBrush}"
6369
BorderThickness="{TemplateBinding BorderThickness}"
64-
CornerRadius="{TemplateBinding Border.CornerRadius}">
65-
<Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
66-
<Grid.ColumnDefinitions>
67-
<ColumnDefinition Width="*" />
68-
<ColumnDefinition Width="Auto" />
69-
</Grid.ColumnDefinitions>
70-
71-
<Grid Grid.Column="0" Margin="{TemplateBinding Padding}">
72-
<Decorator
73-
x:Name="PART_ContentHost"
74-
Margin="0"
75-
VerticalAlignment="Center"
76-
TextElement.Foreground="{TemplateBinding Foreground}" />
77-
</Grid>
78-
</Grid>
79-
</Border>
80-
81-
<!-- The Accent Border is a separate element so that changes to the border thickness do not affect the position of the element -->
82-
<Border
83-
x:Name="AccentBorder"
84-
HorizontalAlignment="Stretch"
85-
VerticalAlignment="Stretch"
86-
BorderBrush="{DynamicResource ControlStrokeColorDefaultBrush}"
87-
BorderThickness="{StaticResource PasswordBoxAccentBorderThemeThickness}"
8870
CornerRadius="{TemplateBinding Border.CornerRadius}" />
71+
72+
<ScrollViewer
73+
x:Name="PART_ContentHost"
74+
Margin="{TemplateBinding BorderThickness}"
75+
CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}"
76+
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
77+
IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
78+
IsTabStop="{TemplateBinding ScrollViewer.IsTabStop}"
79+
Padding="{TemplateBinding Padding}"
80+
TextElement.Foreground="{TemplateBinding Foreground}"
81+
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" />
8982
</Grid>
90-
<ControlTemplate.Triggers>
83+
<ControlTemplate.Triggers>
84+
<Trigger Property="IsMouseOver" Value="True">
85+
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundPointerOver}" />
86+
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushPointerOver}" />
87+
<Setter TargetName="PART_ContentHost" Property="Foreground" Value="{DynamicResource TextControlForegroundPointerOver}" />
88+
</Trigger>
9189
<Trigger Property="IsFocused" Value="True">
92-
<Setter TargetName="AccentBorder" Property="BorderThickness" Value="0,0,0,2" />
93-
<Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlFocusedBorderBrush}" />
90+
<Setter TargetName="ContentBorder" Property="BorderThickness" Value="{StaticResource TextControlBorderThemeThicknessFocused}" />
9491
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundFocused}" />
95-
</Trigger>
96-
<MultiTrigger>
97-
<MultiTrigger.Conditions>
98-
<Condition Property="IsEnabled" Value="True" />
99-
<Condition Property="IsMouseOver" Value="True" />
100-
<Condition Property="IsFocused" Value="False" />
101-
</MultiTrigger.Conditions>
102-
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundPointerOver}" />
103-
</MultiTrigger>
104-
<Trigger Property="IsEnabled" Value="True">
105-
<Setter Property="Cursor" Value="IBeam" />
92+
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushFocused}" />
93+
<Setter TargetName="PART_ContentHost" Property="Foreground" Value="{DynamicResource TextControlForegroundFocused}" />
10694
</Trigger>
10795
<Trigger Property="IsEnabled" Value="False">
10896
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundDisabled}" />
10997
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
110-
<Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
111-
<Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
98+
<Setter TargetName="PART_ContentHost" Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
11299
</Trigger>
113100
</ControlTemplate.Triggers>
114101
</ControlTemplate>

src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Themes/Fluent.Dark.xaml

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,78 +3677,68 @@
36773677
</Setter>
36783678
</Style>
36793679
<Style BasedOn="{StaticResource DefaultPageStyle}" TargetType="{x:Type Page}" />
3680-
<Thickness x:Key="PasswordBoxBorderThemeThickness">1,1,1,1</Thickness>
3680+
<!-- Deprecated PasswordBox Resources ( Used in .NET 9 ) -->
36813681
<Thickness x:Key="PasswordBoxAccentBorderThemeThickness">0,0,0,1</Thickness>
36823682
<Thickness x:Key="PasswordBoxLeftIconMargin">10,8,0,0</Thickness>
36833683
<Thickness x:Key="PasswordBoxRightIconMargin">0,8,10,0</Thickness>
36843684
<Thickness x:Key="PasswordBoxButtonMargin">0,5,4,0</Thickness>
36853685
<Thickness x:Key="PasswordBoxButtonPadding">0,0,0,0</Thickness>
36863686
<sys:Double x:Key="PasswordBoxButtonHeight">24</sys:Double>
36873687
<sys:Double x:Key="PasswordBoxButtonIconSize">14</sys:Double>
3688+
<!-- These rsources are redefined in .NET 10 -->
3689+
<!-- <Thickness x:Key="PasswordBoxBorderThemeThickness">1,1,1,1</Thickness> -->
3690+
<Thickness x:Key="PasswordBoxBorderThemeThickness">1</Thickness>
3691+
<!-- Instead of PasswordBoxThemeMinHeight, we need to use TextControlThemeMinHeight, but modifying that right now
3692+
will affect the other styles as well. Once the remaining styles using it are fixed, will remove this resource -->
3693+
<sys:Double x:Key="PasswordBoxThemeMinHeight">32</sys:Double>
36883694
<ContextMenu x:Key="DefaultPasswordBoxContextMenu">
36893695
<MenuItem Command="ApplicationCommands.Paste" />
36903696
</ContextMenu>
36913697
<Style x:Key="DefaultPasswordBoxStyle" TargetType="{x:Type PasswordBox}">
3692-
<!-- Universal WPF UI focus -->
36933698
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
3694-
<!-- Universal WPF UI ContextMenu -->
36953699
<Setter Property="ContextMenu" Value="{DynamicResource DefaultPasswordBoxContextMenu}" />
36963700
<Setter Property="Foreground" Value="{DynamicResource TextControlForeground}" />
36973701
<Setter Property="CaretBrush" Value="{DynamicResource TextControlForeground}" />
36983702
<Setter Property="Background" Value="{DynamicResource TextControlBackground}" />
3699-
<Setter Property="BorderBrush" Value="{DynamicResource TextControlElevationBorderBrush}" />
3703+
<Setter Property="BorderBrush" Value="{DynamicResource TextControlBorderBrush}" />
37003704
<Setter Property="BorderThickness" Value="{StaticResource PasswordBoxBorderThemeThickness}" />
3701-
<Setter Property="HorizontalAlignment" Value="Stretch" />
3702-
<Setter Property="VerticalAlignment" Value="Center" />
3703-
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
3705+
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
3706+
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
3707+
<Setter Property="HorizontalContentAlignment" Value="Left" />
37043708
<Setter Property="VerticalContentAlignment" Value="Top" />
3705-
<Setter Property="MinHeight" Value="{DynamicResource TextControlThemeMinHeight}" />
3709+
<Setter Property="Cursor" Value="IBeam" />
3710+
<Setter Property="MinHeight" Value="{DynamicResource PasswordBoxThemeMinHeight}" />
37063711
<Setter Property="MinWidth" Value="{DynamicResource TextControlThemeMinWidth}" />
37073712
<Setter Property="Padding" Value="{DynamicResource TextControlThemePadding}" />
3708-
<Setter Property="PasswordChar" Value="" />
37093713
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
3710-
<Setter Property="SnapsToDevicePixels" Value="True" />
3714+
<Setter Property="AllowDrop" Value="True" />
3715+
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst" />
3716+
<Setter Property="PasswordChar" Value="" />
3717+
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
37113718
<Setter Property="OverridesDefaultStyle" Value="True" />
37123719
<Setter Property="Template">
37133720
<Setter.Value>
37143721
<ControlTemplate TargetType="{x:Type PasswordBox}">
37153722
<Grid>
3716-
<Border x:Name="ContentBorder" MinWidth="{TemplateBinding MinWidth}" MinHeight="{TemplateBinding MinHeight}" Padding="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding Border.CornerRadius}">
3717-
<Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
3718-
<Grid.ColumnDefinitions>
3719-
<ColumnDefinition Width="*" />
3720-
<ColumnDefinition Width="Auto" />
3721-
</Grid.ColumnDefinitions>
3722-
<Grid Grid.Column="0" Margin="{TemplateBinding Padding}">
3723-
<Decorator x:Name="PART_ContentHost" Margin="0" VerticalAlignment="Center" TextElement.Foreground="{TemplateBinding Foreground}" />
3724-
</Grid>
3725-
</Grid>
3726-
</Border>
3727-
<!-- The Accent Border is a separate element so that changes to the border thickness do not affect the position of the element -->
3728-
<Border x:Name="AccentBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="{DynamicResource ControlStrokeColorDefaultBrush}" BorderThickness="{StaticResource PasswordBoxAccentBorderThemeThickness}" CornerRadius="{TemplateBinding Border.CornerRadius}" />
3723+
<Border x:Name="ContentBorder" MinWidth="{TemplateBinding MinWidth}" MinHeight="{TemplateBinding MinHeight}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding Border.CornerRadius}" />
3724+
<ScrollViewer x:Name="PART_ContentHost" Margin="{TemplateBinding BorderThickness}" CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" IsTabStop="{TemplateBinding ScrollViewer.IsTabStop}" Padding="{TemplateBinding Padding}" TextElement.Foreground="{TemplateBinding Foreground}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" />
37293725
</Grid>
37303726
<ControlTemplate.Triggers>
3727+
<Trigger Property="IsMouseOver" Value="True">
3728+
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundPointerOver}" />
3729+
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushPointerOver}" />
3730+
<Setter TargetName="PART_ContentHost" Property="Foreground" Value="{DynamicResource TextControlForegroundPointerOver}" />
3731+
</Trigger>
37313732
<Trigger Property="IsFocused" Value="True">
3732-
<Setter TargetName="AccentBorder" Property="BorderThickness" Value="0,0,0,2" />
3733-
<Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlFocusedBorderBrush}" />
3733+
<Setter TargetName="ContentBorder" Property="BorderThickness" Value="{StaticResource TextControlBorderThemeThicknessFocused}" />
37343734
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundFocused}" />
3735-
</Trigger>
3736-
<MultiTrigger>
3737-
<MultiTrigger.Conditions>
3738-
<Condition Property="IsEnabled" Value="True" />
3739-
<Condition Property="IsMouseOver" Value="True" />
3740-
<Condition Property="IsFocused" Value="False" />
3741-
</MultiTrigger.Conditions>
3742-
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundPointerOver}" />
3743-
</MultiTrigger>
3744-
<Trigger Property="IsEnabled" Value="True">
3745-
<Setter Property="Cursor" Value="IBeam" />
3735+
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushFocused}" />
3736+
<Setter TargetName="PART_ContentHost" Property="Foreground" Value="{DynamicResource TextControlForegroundFocused}" />
37463737
</Trigger>
37473738
<Trigger Property="IsEnabled" Value="False">
37483739
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundDisabled}" />
37493740
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
3750-
<Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
3751-
<Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
3741+
<Setter TargetName="PART_ContentHost" Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
37523742
</Trigger>
37533743
</ControlTemplate.Triggers>
37543744
</ControlTemplate>

0 commit comments

Comments
 (0)