WPF PasswordBox提示不起作用

ibps3vxo  于 2023-11-21  发布在  其他
关注(0)|答案(2)|浏览(227)

我尝试在WPF中对PasswordBox进行样式设置,使其可以显示Hint。我在ResourceDictionary中使用了以下代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:BudgetBuddy.Styles">
    
    <Style x:Key="PBHintStyle" TargetType="{x:Type PasswordBox}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="BorderBrush" Value="#FFABADB3"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="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" 
                            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
                            SnapsToDevicePixels="True"
                            CornerRadius="4"
                            Padding="5 2 0 0">
                        <Grid>
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                            <TextBlock x:Name="WARKTEXT" Text="{TemplateBinding Tag}" Foreground="DimGray" Visibility="Collapsed" />
                        </Grid>
                    </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>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsFocused" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Visibility" TargetName="WARKTEXT" Value="Visible"/>
                        </MultiTrigger>
                    </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>

</ResourceDictionary>

字符串
这段代码可以工作,但有一个问题。它显示提示,显示字符作为密码字符,但问题是在字段中键入密码后,它显示提示超过密码字符。
以下是一些截图:


的数据
问题是:



我该如何解决这个问题?提前谢谢你。:)

tnkciper

tnkciper1#

您可以尝试处理PasswordBox的PasswordChanged事件,并根据Password属性是否包含任何字符将其Tag属性设置为“True”或“False”,然后检查触发器中Tag属性的值
在xaml上:

<Style x:Key="PasswordStyle" TargetType="{x:Type PasswordBox}">
            <Setter Property="Tag" Value="False"/>
            <EventSetter Event="PasswordChanged" Handler="OnPasswordChanged"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type PasswordBox}">
                            <Border x:Name="border" CornerRadius="4" BorderBrush="LightGray" BorderThickness="4" Background="Transparent" SnapsToDevicePixels="True">
                                <Grid>
                                    <TextBlock x:Name="PassMessage" HorizontalAlignment="Left" VerticalAlignment="Center" Text="   Password..." FontSize="34" Margin="5,0,5,0" Foreground="LightGray" IsHitTestVisible="False" Visibility="Hidden"/>
                                    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" BorderBrush="Transparent" VerticalScrollBarVisibility="Hidden"/>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Tag" Value="False"/>
                                        <Condition Property="IsKeyboardFocusWithin" Value="False"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Visibility" TargetName="PassMessage" Value="Visible"/>
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

<Grid>
    <PasswordBox x:Name="passwordBox" Style="{StaticResource PasswordStyle}" HorizontalAlignment="Left" Margin="217,256,0,0" VerticalAlignment="Top" Width="450"/>
</Grid>

字符串
关于xaml.cs

private void OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            passwordBox = sender as PasswordBox;
            passwordBox.Tag = (!string.IsNullOrEmpty(passwordBox.Password)).ToString();
        }


希望这有帮助

cbjzeqam

cbjzeqam2#

你可以试试这个:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsFocused" Value="False"/>
        <Condition Property="Text" Value=""/>
    </MultiTrigger.Conditions>
    <Setter Property="Visibility" TargetName="WARKTEXT" Value="Visible"/>
</MultiTrigger>
<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsFocused" Value="False"/>
        <Condition Property="Text" Value="{x:Null}"/>
    </MultiTrigger.Conditions>
    <Setter Property="Visibility" TargetName="WARKTEXT" Value="Visible"/>
</MultiTrigger>

字符串
如果文本不为空或null,将显示提示,否则为否。
希望对你有帮助。

相关问题