【发布时间】: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>
感谢您的建议。
【问题讨论】: