C# 如何更改getFocus上的文本框背景色
我一直试图在getFocus事件中更改WPF应用程序中文本框的背景。在winforms中,我做了类似的事情,效果很好C# 如何更改getFocus上的文本框背景色,c#,wpf,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;
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元素并添加GotFocus事件。 这将在代码隐藏中分配一个新的处理程序。 您必须先强制转换发件人,然后才能访问文本框
private void txtTextBox1_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tbox = sender as TextBox;
tbox.Background = Brushes.Red;
}
如果要将其更改回,还存在LostFocus事件。首先,
TextBox
有一个GotFocus
事件。使用x:Name
指令从代码中访问该框
在xaml中
<TextBox x:Name="TextBox" GotFocus="Handler"/>
但是,在WPF中,还可以选择使用绑定
<TextBox>
<TextBox.Style>
<Style TargetType="Control">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Value="True">
<Setter Property="Background" Value="#E55451"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
这不仅会导致着色行为,而且会在不聚焦时将其还原,并且不需要依赖事件,因此相同的代码可以应用于任何类型的控件
与任何样式一样,这也可以放在要使用的资源字典中,可能附加到类型本身
<!-- Resources under App.xaml. This can also be embedded in a ResourceDictionary -->
<Resources>
<Style x:Key="{x:Type TextBox}" TargetType="Control">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, Mode=OneWay, RelativeSource="{RelativeSource Self}}"
Value="True">
<Setter Property="Background" Value="#E55451"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Resources>
您所需要的只是在您的焦点事件中将其更改为该哈希代码
((TextBox)sender).Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#E55451"));
在XAML中查找元素并添加事件,即:
<TextBox Height="23" Name="textBox1" Width="120" GotFocus="textBox1_GotFocus" LostFocus="textBox1_LostFocus" />
最好的方法是在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>
非常感谢。这对我很有效。然而,如果我的表单上有很多文本框,有没有一种方法可以在不必为每个文本框编写代码的情况下为所有文本框都这样做呢?是的,这就是风格之美!我会把它整合到我的答案中。请注意,@pikciu也有一个很好的答案,它将DataTriggers简化为Triggers,我没有尽可能地意识到这一点。我建议你把他的代码用在风格上。
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
textBox1.Background = Brushes.Yellow;
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
textBox1.Background = Brushes.White;
}
<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>