relliv
relliv

Reputation: 853

WPF PasswordBox Rounded Corners

I use it to round the corners of TextBox,

    <ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
        <Border Background="{TemplateBinding Background}" 
            x:Name="Bd" BorderBrush="#FFE6DDDD"
            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">
            <ScrollViewer x:Name="PART_ContentHost"/>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
            <Trigger Property="Width" Value="Auto">
                <Setter Property="MinWidth" Value="100"/>
            </Trigger>
            <Trigger Property="Height" Value="Auto">
                <Setter Property="MinHeight" Value="20"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

And I apply,

        <TextBox Template="{StaticResource TextBoxBaseControlTemplate}"
             Height="25"
             Margin="168,100,139,194"
             HorizontalContentAlignment="Center"
             VerticalContentAlignment="Center"
             Background="{x:Null}"
             BorderBrush="{x:Null}"
             FontFamily="Aller Light">              
        </TextBox>

Mission completed

enter image description here

Then I want to do in PasswordBox,

    <ControlTemplate x:Key="passwordbox" TargetType="{x:Type PasswordBox}">
        <Border Background="{TemplateBinding Background}" 
            x:Name="Bd" BorderBrush="#FFE6DDDD"
            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
            <Trigger Property="Width" Value="Auto">
                <Setter Property="MinWidth" Value="100"/>
            </Trigger>
            <Trigger Property="Height" Value="Auto">
                <Setter Property="MinHeight" Value="20"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

apply

    <PasswordBox Template="{StaticResource passwordbox}"
                 Height="25"
                 Margin="168,140,139,154"
                 HorizontalContentAlignment="Center"
                 VerticalContentAlignment="Center"
                 Background="{x:Null}"
                 Password="someonepass">
    </PasswordBox>

it seems to be successful, but the content can not be entered. (second box)

enter image description here

if I delete template, is normally

enter image description here

How to fix it? Thanks...

Upvotes: 1

Views: 7995

Answers (1)

Il Vic
Il Vic

Reputation: 5666

Your PasswordBox control template misses a part named "PART_ContentHost" (tipically a ScrollViewer). Take as a sample your TextBoxBase template.

So your temaplate should be:

<ControlTemplate x:Key="passwordbox" TargetType="{x:Type PasswordBox}">
    <Border Background="{TemplateBinding Background}" 
        x:Name="Bd" BorderBrush="#FFE6DDDD"
        BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">
        <ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
        <Trigger Property="Width" Value="Auto">
            <Setter Property="MinWidth" Value="100"/>
        </Trigger>
        <Trigger Property="Height" Value="Auto">
            <Setter Property="MinHeight" Value="20"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

I hope it can help you.

Upvotes: 5

Related Questions