为什么PasswordBox没有圆角样式?

发布于 2025-01-16 02:03:27 字数 1310 浏览 5 评论 0原文

我为 TextBox 圆角编写了一个样式代码,它起作用了。但是当我尝试相同的代码时,只需将 TargetType 字段更改为 PasswordBox ,它对 PasswordBox 不起作用。 提前致谢。

这是我的样式代码:

<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>

这是我的 XAML 样式绑定代码:

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

这是插入样式代码后我的窗口的外观。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2025-01-23 02:03:27

PasswordBox 的特殊之处在于它重置 Border 样式。它是这样实现的。您可以做的是复制PasswordBox的默认样式并直接调整圆角半径。

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文