WPF 中的密码框提示文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27166893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:44:23  来源:igfitidea点击:

PasswordBox Hint Text in WPF

wpfxaml

提问by MLElyakan

I wrote a PasswordBox style.I want to do hint text in passwordbox but when the lost focus ,the hint text is appering.Where I'm doing mistake, How can I solve this problem?

我写了一个密码框样式。我想在密码框中做提示文本,但是当失去焦点时,提示文本出现了。我做错的地方,我该如何解决这个问题?

Here is code:

这是代码:

 <Style TargetType="{x:Type PasswordBox}"  xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Style.Resources>
        <VisualBrush  x:Key="Bg1" AlignmentX="Center" AlignmentY="Center" Stretch="None" >
            <VisualBrush.Visual>
                <Label Content="Enter password" Foreground="LightGray" Margin="5,0,0,0"/>
            </VisualBrush.Visual>
        </VisualBrush>
        <VisualBrush  x:Key="Bg2" AlignmentX="Center" AlignmentY="Center" Stretch="None" >
            <VisualBrush.Visual>
                <Label Content="" Foreground="LightGray" Margin="5,0,0,0"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Style.Resources>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="Foreground" Value="#FF585858"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type PasswordBox}">
                <Border CornerRadius="2" x:Name="border" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="LightGray">
                    <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="0" ScrollViewer.CanContentScroll="True" x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="BorderBrush" TargetName="border" Value="#FFD0D0D0"/>
                        <Setter Property="BorderThickness" TargetName="border" Value="2"/>
                    </Trigger>

                    <DataTrigger Binding="{Binding Path=Password}" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background" Value="{StaticResource Bg2}" />
                    </DataTrigger>

                    <DataTrigger Binding="{Binding Path=Password}" Value="{x:Null}">
                        <Setter Property="Background" Value="{StaticResource Bg1}" />
                    </DataTrigger>                      

                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="White" />
                    </Trigger>                      

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>   

Thanks for your advice.

谢谢你的建议。

回答by Jawahar

Password property in PasswordBox is not a Dependency Property for some security reasons. So it cannot be used in Trigger. The triggers you applied for Password property were not working. That is the reason, the hint text is appearing even if you have valid text in it. Only the trigger applied on IsKeyboardFocused is working, regardless of whether Password is empty or NULL.

出于某些安全原因,PasswordBox 中的密码属性不是依赖属性。所以不能在Trigger中使用。您为 Password 属性应用的触发器不起作用。这就是原因,即使您有有效文本,提示文本也会出现。只有应用在 IsKeyboardFocused 上的触发器才起作用,无论 Password 为空还是 NULL。

The following post must be giving some good idea to implement watermark for Password box,

以下帖子必须提供一些为密码框实施水印的好主意,

http://prabu-guru.blogspot.in/2010/06/how-to-add-watermark-text-to-textbox.html

http://prabu-guru.blogspot.in/2010/06/how-to-add-watermark-text-to-textbox.html