我一直在尝试在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应用程序中完成这项工作?
答案 0 :(得分:1)
首先,TextBox
有GotFocus
个事件。使用x:Name
指令使代码框可以访问...
在xaml ......
<TextBox x:Name="TextBox" GotFocus="Handler"/>
然后在代码隐藏......
private void Handler(object sender, RoutedEventArgs args)
{
TextBox.Background = new SolidColorBrush(Color.FromArgb(...));
}
但是,在WPF中,还可以选择使用Binding
<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>
答案 1 :(得分:1)
Imho最好的方法是在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>
答案 2 :(得分:0)
只需转到XAML中的TextBox元素并添加GotFocus事件即可。 这将在后面的代码中分配一个新的处理程序。 您必须先发送发件人,然后才能访问TextBox。
private void txtTextBox1_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tbox = sender as TextBox;
tbox.Background = Brushes.Red;
}
如果你想要改变它,还有一个LostFocus事件。
答案 3 :(得分:0)
您需要的只是在您的重点事件中将其更改为该哈希代码
((TextBox)sender).Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#E55451"));
答案 4 :(得分:0)
在XAML中查找元素并添加事件,即:
#-------------------------------------------------------------------------
# Warmup.ps1
#-------------------------------------------------------------------------
[int]$returnme = 0
[int]$SleepTime = 5
[string]$regex = '[>"]?[aA]vailable["<]?'
[string]$strContains = $regex
# [string]$strContains = "log in"
[string]$hostName = hostname
[string]$domainName = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .).DNSDomain
[string]$warmMeUp = "http://$hostName.$domainName/endpoint"
[string]$html = "Will Be Set Later"
#-------------------------------------------------------------------------
# Get-WebPage
#-------------------------------------------------------------------------
function Get-WebPage([string]$url)
{
try
{
$wc = new-object net.webclient;
$wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;
[string]$pageContents = $wc.DownloadString($url);
$wc.Dispose();
}
catch
{
Write-Host "First Try Failed. Second Try in $SleepTime Seconds."
try
{
Start-Sleep -s $SleepTime
$wc = new-object net.webclient;
$wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;
$pageContents = $wc.DownloadString($url);
$wc.Dispose();
}
catch
{
$pageContents = GetWebSiteStatusCode($url)
}
}
return $pageContents;
}
#-------------------------------------------------------------------------
# GetWebSiteStatusCode
#-------------------------------------------------------------------------
function GetWebSiteStatusCode
{
param (
[string] $testUri,
[int] $maximumRedirection = 5
)
$request = $null
try {
$request = Invoke-WebRequest -Uri $testUri -MaximumRedirection $maximumRedirection -ErrorAction SilentlyContinue
}
catch [System.Net.WebException] {
$request = $_.ErrorDetails.Message
}
catch {
Write-Error $_.Exception
return $null
}
return $request
}
#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
"Warming up '{0}'..." -F $warmMeUp;
$html = Get-WebPage -url $warmMeUp;
Write-Host "Looking for Pattern $strContains"
if ($html.ToLower().Contains("unavailable") -or !($html -match $strContains))
{
$returnme = -1
Write-Host "Warm Up Failed. html returned:`n" + $html
}
exit $returnme
并设置颜色:
<TextBox Height="23" Name="textBox1" Width="120" GotFocus="textBox1_GotFocus" LostFocus="textBox1_LostFocus" />