c# - 如何更改 getFocus 上的文本框背景颜色

标签 c# wpf

我一直在尝试通过 getFocus 事件更改 WPF 应用程序中文本框的背景。在winforms中我做了类似的事情并且效果很好。

    class Format_GUI
        {
            public void center_groupbox(Control cntrl, Form ms_form)
              {
                cntrl.Left = (ms_form.ClientSize.Width - cntrl.Width) / 2;

              }


           public void color_control(Control myControl)
              {
                Control inst_Control = new Control();
                inst_Control = myControl;
                inst_Control.BackColor =   System.Drawing.ColorTranslator.FromHtml("#E55451");
              }

}

然后我可以在 Enter 事件上调用类函数,如下所示:

    private void txtTextBox1_Enter(object sender, EventArgs e)
    {
        myGUI.highlight_SelectedControl(txtTextBox1);
    }

这在 WinForms 中工作得很好。有谁知道如何在 WPF 应用程序中完成此操作?

最佳答案

恕我直言,最好的方法是在 XAML 中使用触发器

<TextBox>
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="TextBox.IsFocused"
                             Value="True">
                        <Setter Property="TextBox.Background"
                                Value="#E55451" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

关于c# - 如何更改 getFocus 上的文本框背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34052229/

相关文章:

c# - 处理后退按钮,在 WP7 中更改页面

c# - 断言在 C# WPF 应用程序的 Visual Studio 2010 中不起作用

wpf - 在wpf MVVM中处理窗口关闭按钮

c# - 自动化从一个文件夹中的多个 CSV 文件导出数据的过程

c# - 从集合中按索引获取一组项目的最优雅方法是什么?

c# - 在 Windows Phone 8.1 上捕获转储

c# - 委托(delegate)事件的模式

c# - 违反通用参数约束?

c# - 更新绑定(bind)到它的 ObservableCollection 后,DataGrid 不会更新

c# - C# 中加载数据 infile 查询时出错