wpfwpf-controlsrounded-cornerswpf-stylepasswordbox

Why PasswordBox is not getting a rounded corner style?


I wrote a style code for TextBox rounded corner and it worked. But When I tried the same code just changing the TargetType field to PasswordBox It didn't work for PasswordBox. Thanks in advance.

This is my style code:

<Style x:Key="TextBoxTemplate" TargetType="TextBox">
    <Setter Property="Background" Value="#525252"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="BorderThickness" Value="0,0,0,1"/>
    <Style.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="10"/>
        </Style>
    </Style.Resources>
</Style>

<Style x:Key="PasswordBoxTemplate" TargetType="PasswordBox">
    <Setter Property="Background" Value="#525252"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="BorderThickness" Value="0,0,0,1"/>
    <Style.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="10"/>
        </Style>
    </Style.Resources>
</Style>

This is my XAML style binding code:

<PasswordBox Style="{DynamicResource PasswordBoxTemplate}" Name="txtPassword" FontSize="18"/>

This is how my window look after inserting the style code.


Solution

  • The PasswordBox is special in that it resets the Border style. It is implemented this way. What you can do is you can copy the default style of the PasswordBox and adapt the corner radius directly.

    <Style x:Key="PasswordBoxStyle" TargetType="{x:Type PasswordBox}">
       <Setter Property="PasswordChar" Value="●"/>
       <Setter Property="Background" Value="#525252"/>
       <Setter Property="BorderBrush" Value="#FFABAdB3"/>
       <Setter Property="Foreground" Value="White"/>
       <Setter Property="BorderThickness" Value="0,0,0,1"/>
       <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
       <Setter Property="HorizontalContentAlignment" Value="Left"/>
       <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
       <Setter Property="AllowDrop" Value="true"/>
       <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
       <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
       <Setter Property="Template">
          <Setter.Value>
             <ControlTemplate TargetType="{x:Type PasswordBox}">
                <Border x:Name="border" CornerRadius="10" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="True">
                   <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                </Border>
                <ControlTemplate.Triggers>
                   <Trigger Property="IsEnabled" Value="false">
                      <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                   </Trigger>
                   <Trigger Property="IsMouseOver" Value="true">
                      <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                   </Trigger>
                   <Trigger Property="IsKeyboardFocused" Value="true">
                      <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                   </Trigger>
                </ControlTemplate.Triggers>
             </ControlTemplate>
          </Setter.Value>
       </Setter>
       <Style.Triggers>
          <MultiTrigger>
             <MultiTrigger.Conditions>
                <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                <Condition Property="IsSelectionActive" Value="false"/>
             </MultiTrigger.Conditions>
             <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
          </MultiTrigger>
       </Style.Triggers>
    </Style>