WPF Xaml更改TextBox触发器更改前景

问题描述 投票:1回答:1

我有一个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;            

}
c# wpf xaml triggers
1个回答
1
投票

这应该是你正在寻找的:

<Trigger Property="IsKeyboardFocused" Value="True">
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="FontWeight" Value="Medium"/>
</Trigger>

PreviewMouseDown不是财产,它是一个事件,这就是你收到信息的原因。 IsKeyboardFocused是一个应该完成你想要的财产。有关属性列表,请参阅TextBox

注意:一旦用户离开焦点,这也会将文本设置为灰色。如果这不是你想要的,请告诉我,我会更新这个答案。

© www.soinside.com 2019 - 2024. All rights reserved.