2013-03-21 82 views
-1

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

+0

你尝试过这么远吗?它是一个您正在使用的RichTextBox?这支持文本高亮显示。 – RainbowFish 2013-03-21 10:07:03

+0

可能的重复http://stackoverflow.com/questions/12094937/how-to-highlight-select-text-in-a-wpf-textbox-without-focus – Klaus78 2013-03-21 10:33:13

+0

可能的,但没有答案 – Khmel 2013-03-21 10:45:11

回答

0

如果你处理你的TextBox的LostFocus事件,你可以使用下面的代码来选择文本框的内容:

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

这不起作用,当窗口不活动时,选定的文本不会突出显示。 **我可以重写OnLostKeyboardFocus,但它是一个!** – Khmel 2013-03-21 10:44:02

+0

回到我原来的评论,那么,到目前为止您尝试过什么?提供与您已经尝试的步骤相同的答案对我们来说都是浪费时间! – RainbowFish 2013-03-21 11:07:16

+0

我试图重写文本框控件模板 – Khmel 2013-03-21 11:12:51

0

您可以使用样式与EventTrigger为TextBox.LostFocus/GotFoxus事件。

这将改变文本框前景色到红用1秒的延迟,当“引发LostFocus”是“真”

<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> 

+0

对于你的答案,但我不想改变所有文字的颜色,我只想突出显示选定的文字。 – Khmel 2013-03-21 18:52:20

+0

在我提供的样式中,有一个“TextBox.GotFocus”,只有在选择/获得文本焦点时才会运行。这应该适合你,否则我不明白你想完成什么。 – hijack 2013-03-22 08:30:13

+0

我希望[这张图片](https://dl.dropbox.com/u/20573569/Jpg/Untitled.jpg)可以帮助你理解这个问题 – Khmel 2013-03-22 18:34:59

相关问题