水印文本框的 WPF 水印密码框
我正在使用水印文本框,如 WPF 中的水印文本框
<Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}" Style="{StaticResource EntryFieldStyle}" >
<TextBlock Margin="5,2" Text="This prompt dissappears as you type..." Foreground="{StaticResource brushWatermarkForeground}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Name="txtUserEntry" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" />
</Grid>
我如何将其应用于密码框?
I am using a Watermark textbox as in Watermark TextBox in WPF
<Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}" Style="{StaticResource EntryFieldStyle}" >
<TextBlock Margin="5,2" Text="This prompt dissappears as you type..." Foreground="{StaticResource brushWatermarkForeground}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Name="txtUserEntry" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" />
</Grid>
How can I apply this for a PasswordBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般方法是相同的:编写自定义控件样式,并在密码框为空时显示水印。这里唯一的问题是
PasswordBox.Password
不是依赖属性,您不能在触发器中使用它。此外,PasswordBox 是密封的,因此您无法覆盖此通知行为,但您可以在此处使用附加属性。以下代码演示了如何操作。
XAML:
C#:
请注意 XAML 代码中的
PasswordBoxMonitor
。The general approach is the same: write a custom control style, and show the watermark whenever the password box is empty. The only problem here is that
PasswordBox.Password
is not a dependency property, and you can't use it in a trigger. Also PasswordBox is sealed, so you can't override this notification behavior, but you can use attached properties here.The following code demonstrates how.
XAML:
C#:
Please notice
PasswordBoxMonitor
in XAML code.您可以自己显示/隐藏背景,而不是使用触发器:
XAML:
代码隐藏:
you can show/hide the background by yourself instead of using triggers:
XAML:
Code behind:
您可以使用我的方法来实现水印行为。您所要做的就是复制并粘贴
TextBoxWatermarkBehavior
并将Behavior
更改为Behavior
。您可以在此处找到演示项目
you can use my approach for a watermark behavior. all you have to do is copy and paste the
TextBoxWatermarkBehavior
and the change theBehavior<TextBox>
toBehavior<PasswordBox>
.you can find a demo project here
@blindmeis 的建议很好。对于PasswordBox,该类如下。
@blindmeis's suggestion is good. For PasswordBox the class would be as follows.
Werner Wichtig 的方法简单又好,但不适用于带有某些文本的
Background
属性。我已将它与Style
属性一起使用。PasswordBox
> 的 XAML 代码Style
资源:您的目标
PasswordBox
代码:在
PasswordBox
的PasswordChanged
事件上实现的代码隐藏逻辑:这是最终的结果:
Werner Wichtig 's approach is simple and good, but not applicable on
Background
property with some text. I have used it withStyle
property.XAML code for
PasswordBox
>Style
resource:Your targeted
PasswordBox
code:Code-behind logic implemented on
PasswordChanged
event ofPasswordBox
:Here is final result: