,我认为这会工作,我想你想实现你的TextBox
<Style x:Key="CustomTextBoxStyle" TargetType="TextBox">
<Setter Property="Background" Value="#FF22252C" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Width" Value="200" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border CornerRadius="5" Background="#FF22252C" Margin="3" MinWidth="{TemplateBinding MinWidth}">
<Grid>
<StackPanel Margin="8">
<ScrollViewer x:Name="PART_ContentHost"/>
</StackPanel>
<StackPanel>
<TextBlock Name="PART_TempText" Text="{TemplateBinding Name}" Foreground="#FF454954"
Visibility="Collapsed"
Padding="8" />
</StackPanel>
</Grid>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Text.Count, RelativeSource={RelativeSource Self}}" Value="0">
<Setter TargetName="PART_TempText" Property="Visibility" Value="Visible" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
的想法一样的功能的占位符/水印是你隐藏TextBlock
开始,如果TextBox
“ s Text.Count
为0(表示未输入任何值),则显示TextBlock
。
更新
我对你提到PasswordBox
的问题的解决方案,也许它不是最漂亮的(这不是),但无论如何,我会分享:)
为什么它不”的原因将不起作用,这是因为
一个可能的解决方案是:
一个新的附加属性添加到您的项目:
public class PasswordBoxAttachedProperties
{
public static readonly DependencyProperty IsPasswordEnteredProperty = DependencyProperty.RegisterAttached(
"IsPasswordEntered", typeof (bool), typeof (PasswordBoxAttachedProperties), new PropertyMetadata(default(bool)));
public static void SetIsPasswordEntered(DependencyObject element, bool value)
{
element.SetValue(IsPasswordEnteredProperty, value);
}
public static bool GetIsPasswordEntered(DependencyObject element)
{
return (bool) element.GetValue(IsPasswordEnteredProperty);
}
}
变化触发了PasswordBox
的Style
以下几点:
<DataTrigger Binding="{Binding (local:PasswordBoxAttachedProperties.IsPasswordEntered), RelativeSource={RelativeSource Self}}" Value="False">
<Setter TargetName="PART_TempText" Property="Visibility" Value="Visible" />
</DataTrigger>
local
是您在您的应用中使用的命名空间映射lication。
添加到System.Windows.Interactivity参考,并创建以下TriggerAction
:
public class NotifyPasswordChangeTrigger : TriggerAction<PasswordBox>
{
protected override void Invoke(object parameter)
{
AssociatedObject.SetValue(PasswordBoxAttachedProperties.IsPasswordEnteredProperty, !string.IsNullOrEmpty(AssociatedObject.Password));
}
}
最后,在PasswordBox
添加此触发:
<PasswordBox Name="Password">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PasswordChanged">
<local:NotifyPasswordChangeTrigger />
</i:EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>
PS:我不认为您应该使用Name
属性作为占位符/水印。也许您应该为创建一个新的附加属性,所以你可以使用它像这样(和Name
在您的样式,当然更换绑定到新的附加属性):
<PasswordBox local:TextBoxAttachedProperties.Placeholder="Please enter password...">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PasswordChanged">
<local:NotifyPasswordChangeTrigger />
</i:EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>
这非常的感谢!之前没有这样做,所以不太确定该怎么做! –
嗯,不工作在我的密码箱,我已经明显改变它,就像这样:''但它不工作 –
啊,密码被密封并且触发器不起作用:( –