WPF PasswordBox 圆角

Ego*_*per 0 wpf xaml

我用它来圆化 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>
Run Code Online (Sandbox Code Playgroud)

我申请,

        <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>
Run Code Online (Sandbox Code Playgroud)

任务完成

在此处输入图片说明

然后我想在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>
Run Code Online (Sandbox Code Playgroud)

申请

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

好像成功了,但是内容进不去。(第二个盒子)

在此处输入图片说明

如果我删除模板,通常是

在此处输入图片说明

如何解决?谢谢...

Il *_*Vic 5

您的PasswordBox控件模板缺少名为“PART_ContentHost”的部分(通常为 a ScrollViewer)。以您的TextBoxBase模板为例。

所以你的模板应该是:

<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>
Run Code Online (Sandbox Code Playgroud)

我希望它能帮助你。