如何在文本框未聚焦(键盘焦点丢失)时突出显示wpf中的文本?

时间:2013-03-21 10:04:18

标签: .net wpf xaml textbox highlight

如何在文本框未聚焦时突出显示Wpf中的文本? (.NET 4.0)

2 个答案:

答案 0 :(得分:0)

如果处理TextBox的LostFocus事件,可以使用以下代码选择TextBox的内容:

textBox.SelectAll();
e.Handled = true;

答案 1 :(得分:0)

您可以将带有EventTrigger的Style用于TextBox.LostFocus / GotFoxus事件。

当“LostFocus”为“true”时,这会将TextBox Foreground更改为红色,延迟时间为1秒

 <Style x:Key="tboxStandard"
     TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness"
        Value="2" />
<Setter Property="BorderBrush"
        Value="#292929" />
<Setter Property="Background"
        Value="#E9E9E9" />
<Setter Property="TextAlignment"
        Value="Center" />
<Setter Property="Foreground"
        Value="#191919" />
<Style.Triggers>
  <EventTrigger RoutedEvent="TextBox.GotFocus">
    <EventTrigger.Actions>
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)"
                          To="#191919"
                          Duration="0:0:1" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger.Actions>
  </EventTrigger>
  <EventTrigger RoutedEvent="TextBox.LostFocus">
    <EventTrigger.Actions>
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)"
                          To="Red"
                          Duration="0:0:1" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger.Actions>
  </EventTrigger>
</Style.Triggers>