<TextBox x:Name="txtCount" Width="200" Height="35" GotFocus="txtCount_GotFocus"


LostFocus
="txtCount_LostFocus" ></TextBox>

然后在后台cs文件中分别对OnGotFocus和OnLostFocus两个方法做处理:

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->private void txtCount_GotFocus(object sender, RoutedEventArgs e)

{

      this.txtCount.Foreground = new SolidColorBrush(Colors.Red);
}

 

private void txtCount_LostFocus(object sender, RoutedEventArgs e)

{
    this.txtCount.Foreground = new SolidColorBrush(Colors.LightGray);

}

 

搞定之后,运行,当鼠标焦点落在txtCount上时,文字呈红色:

  当焦点移开后,文字呈浅灰色: 

 

MSDN 查阅:

https://msdn.microsoft.com/zh-cn/library/system.windows.controls.textbox_events(v=vs.95).aspx

 

 

  名称 说明
BindingValidationError FrameworkElement。)
DataContextChanged FrameworkElement。)
DoubleTap UIElement。)
DragEnter UIElement。)
DragLeave UIElement。)
DragOver UIElement。)
Drop UIElement。)
GotFocus UIElement。)
Hold UIElement。)
IsEnabledChanged Control。)
KeyDown UIElement。)
KeyUp UIElement。)
LayoutUpdated FrameworkElement。)
Loaded FrameworkElement。)
LostFocus UIElement。)
LostMouseCapture UIElement。)
ManipulationCompleted UIElement。)
ManipulationDelta UIElement。)
ManipulationStarted UIElement。)
MediaCommand UIElement。)
MouseEnter UIElement。)
MouseLeave UIElement。)
MouseLeftButtonDown UIElement。)
MouseLeftButtonUp UIElement。)
MouseMove UIElement。)
MouseRightButtonDown UIElement。)
MouseRightButtonUp UIElement。)
MouseWheel UIElement。)
SelectionChanged 在文本选定内容更改后发生。
SizeChanged FrameworkElement。)
Tap UIElement。)
TextChanged 在文本框中的内容更改时发生。
TextInput UIElement。)
TextInputStart UIElement。)
TextInputUpdate UIElement。)
Unloaded FrameworkElement。)

 

相关文章: