[英]WPF Xaml Change TextBox Trigger Change Foreground
我有一个文本框样式。 我正在尝试制作一个占位符(我意识到我不是第一个问这个问题的人。)但是我发现了一种非常简单的方法可以满足我的需求。 一旦用户单击该框,“电子邮件”就会被删除。
public void email_input_Click(object sender, System.EventArgs e)
{
if(email_input.Text == "email")
{
email_input.Text = "";
}
}
现在是字体。 我的默认文本颜色是灰色。 当用户开始打字时,我希望它变成黑色。 我是 xaml 和 wpf 的新手,无法找出这样做的触发器。
<!-- Placeholder -->
<Style x:Key="PlaceHolder" TargetType="TextBox">
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Width" Value="340"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="0.5"/>
<Setter Property="FontWeight" Value="Light"/>
<Style.Triggers>
<Trigger Property="PreviewMouseDown" Value="True">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontWeight" Value="Medium"/>
</Trigger>
</Style.Triggers>
</Style>
Property="PreviewMouseDown" 无法识别或无法访问。 为什么它不可访问,我可以使用什么触发器?
编辑:这似乎有效,但我不确定它有多健壮。
public void email_input_Click(object sender, System.EventArgs e)
{
if(email_input.Text == "email")
{
email_input.Text = "";
}
email_input.Foreground = Brushes.Black;
email_input.FontWeight = FontWeights.SemiBold;
}
这应该是你要找的:
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontWeight" Value="Medium"/>
</Trigger>
PreviewMouseDown
不是属性,而是事件,这就是您收到消息的原因。 IsKeyboardFocused
是一个应该完成您想要的属性。 有关属性列表,请参阅TextBox 。
注意:一旦用户离开焦点,这也会将文本设置回灰色。 如果这不是您想要的,请告诉我,我会更新此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.