-1

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

4

2 回答 2

0

您可以将 Style 与 EventTrigger 一起用于 TextBox.LostFocus/GotFoxus 事件。

当“LostFocus”为“true”时,这会将 TextBox 前景更改为红色,延迟 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>

于 2013-03-21T15:42:00.900 回答
0

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

textBox.SelectAll();
e.Handled = true;
于 2013-03-21T10:31:23.393 回答