【问题标题】:PasswordBox Hint Text in WPFWPF 中的 PasswordBox 提示文本
【发布时间】:2014-11-27 09:16:22
【问题描述】:

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

代码如下:

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

感谢您的建议。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

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

    下面的帖子一定是为密码框实现水印提供了一些好主意,

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

    【讨论】:

    • 谢谢回复。不使用后面的代码可以吗。是不是一定要写cs代码?
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多