WPF 无法使密码框元素的角变圆
Posted
技术标签:
【中文标题】WPF 无法使密码框元素的角变圆【英文标题】:WPF Cannot make the passwordbox element corners rounded 【发布时间】:2021-10-19 02:10:53 【问题描述】:我正在尝试将我的 PasswordBox 角弄圆,使其看起来更现代。我已经用我的按钮和 TexBlock 成功地做到了。但是,尝试使用我的 PasswordBox 执行此操作并不会产生我想要的相同结果。
PasswordBox 的 xaml 代码如下所示
<PasswordBox
Grid.Column="1"
Grid.Row="5"
x:Name="paswordinput"
Margin="170,0,0,0">
<PasswordBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="8"></Setter>
</Style>
</PasswordBox.Resources>
</PasswordBox>
相同的代码适用于我的 TextBox 和我的 Button。任何帮助将不胜感激!
【问题讨论】:
你可以根据微软文档重新设置它的样式 - docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/… 【参考方案1】:PasswordBox
控件清除了Border
的Style
,因此您应该在模板中设置CornerRadius
属性的本地值:
<PasswordBox
Grid.Column="1"
Grid.Row="5"
x:Name="paswordinput"
Margin="170,0,0,0">
<PasswordBox.Style>
<Style TargetType="x:Type PasswordBox">
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="8" />
</Style>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type PasswordBox">
<Border x:Name="border" CornerRadius="8" BorderBrush="TemplateBinding BorderBrush" BorderThickness="TemplateBinding BorderThickness" Background="TemplateBinding Background" 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>
</PasswordBox.Style>
</PasswordBox>
【讨论】:
【参考方案2】:希望这篇博文对您有所帮助 - https://www.kailashsblogs.com/2017/01/round-corner-passwordbox-in-wpf.html
使用下面的代码 -
<PasswordBox Margin="10" Height="50" Width="200">
<PasswordBox.Template>
<ControlTemplate TargetType="PasswordBox">
<Border CornerRadius="10" BorderBrush="Gray" BorderThickness="1" />
</ControlTemplate>
</PasswordBox.Template>
</PasswordBox>
【讨论】:
【参考方案3】:您也可以尝试使用以下代码为密码设置圆角。更多详情请参考here。
<PasswordBox x:Name="paswordinput" Width="100" Height="50">
<PasswordBox.Resources>
<Style TargetType="PasswordBox">
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<Border CornerRadius="10" BorderBrush="Gray" BorderThickness="2" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</PasswordBox.Resources>
</PasswordBox>
【讨论】:
以上是关于WPF 无法使密码框元素的角变圆的主要内容,如果未能解决你的问题,请参考以下文章