密码框上的样式触发器 [英] Style Trigger on Passwordbox
本文介绍了密码框上的样式触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文本框上使用以下样式,以便在有人尝试向其中输入数据之前,它具有文本和背景颜色.工作正常,但出现了我的问题,因为它是一个登录屏幕,而我的另一个控件是一个 Passwordbox,它不允许我访问 Password 属性(相当于 Textbox 的 Text 属性).关于我如何解决这个问题的任何建议?
I'm using the following style on a Textbox so that it has text and a background colour until someone tries to enter data into it. Works fine but my problem arises because it is a login screen and my other control is a Passwordbox which won't let me access the Password property (which is the equivalent to the Text property of the Textbox). Any advice on how I would work around this?
<Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="Search" Foreground="LightGray"/>
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
<Setter Property="Control.Foreground" Value="#4C2C66"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
推荐答案
如果您使用的是 PasswordBox
,请确保更改 TargetType
并使用 数据触发器
:
If you are using a PasswordBox
, make sure to change the TargetType
and use a DataTrigger
:
<Style TargetType="{x:Type PasswordBox}" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="Search" Foreground="LightGray"/>
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Password}" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</DataTrigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
<Setter Property="Control.Foreground" Value="#4C2C66"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
这篇关于密码框上的样式触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文