【问题标题】:WPF Cannot make the passwordbox element corners roundedWPF 无法使密码框元素的角变圆
【发布时间】: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。任何帮助将不胜感激!

【问题讨论】:

标签: c# wpf visual-studio xaml


【解决方案1】:

PasswordBox 控件清除了BorderStyle,因此您应该在模板中设置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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 2016-10-13
        • 1970-01-01
        • 2017-07-26
        • 1970-01-01
        相关资源
        最近更新 更多