【发布时间】:2018-08-08 13:25:43
【问题描述】:
我有一个 TextBox 样式。我正在尝试制作一个占位符(我意识到我不是第一个提出这个问题的人。)但是我发现了一种非常简单的方法可以满足我的需求。一旦用户在框中单击,“电子邮件”就会被删除。
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;
}
【问题讨论】: