代码之家  ›  专栏  ›  技术社区  ›  Steve Cadwallader

如何更改WPF文本框的突出显示文本颜色?

  •  13
  • Steve Cadwallader  · 技术社区  · 16 年前

    TextBox

    对于 ListBoxItem s、 有一个 neat trick HighlightBrushKey 要自定义聚焦设置中的系统高亮显示颜色,请执行以下操作:

      <Style TargetType="ListBoxItem">
        <Style.Resources>
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen"/>
        </Style.Resources>
      </Style>
    

    同样的伎俩对女人不起作用 ControlTemplate "?

    谢谢你的建议!

    NOTE: This behavior appears to be added to WPF 4.

    5 回复  |  直到 13 年前
        1
  •  11
  •   Colonel Panic    4 年前

    从.NET4开始, TextBoxBase.SelectionBrush

    通过设置SelectionBrush和SelectionCapacity属性,可以指定高亮显示选定文本的笔刷。SelectionCapacity属性指定SelectionBrush的不透明度。

    <TextBox SelectionBrush="Red" SelectionOpacity="0.5"
             Foreground="Blue" CaretBrush="Blue">  
    
        2
  •  10
  •   Nash    13 年前

    正如Steve提到的: NOTE: This behavior appears to be added to WPF 4.

    正如WPF博士所说

    他说:"这是完全不可能的 测试版)。该控件硬编码为 请看一下控制模板。”

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bbffa6e3-2745-4e72-80d0-9cdedeb69f7f/

        3
  •  0
  •   Pollitzer    9 年前

    这是一个Windows 8.1.Net 4.6.1测试解决方案,用于自定义 SelectionBrush TextBox 在应用程序中:

    /// Constructor in App.xaml.cs
    public App() : base()
    {
        // Register an additional SelectionChanged handler for appwide each TextBox
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.SelectionChangedEvent, RoutedEventHandler(_textBox_selectionChanged));
    }
    
    private void _textBox_selectionChanged(object sender, RoutedEventArgs e)
    {
        // Customize background color of selected text
        (sender as TextBox).SelectionBrush = Brushes.MediumOrchid;
    
        // Customize opacity of background color
        (sender as TextBox).SelectionOpacity = 0.5;
    }
    

    RichTextBox 文本框 四次 TextBoxBase

        4
  •  -1
  •   Jobi Joy    16 年前

    您可以为文本框创建样式,并为背景编写Setter。TextBox样式应该是默认样式,这样任何位于可视树下的TextBox都将获得更改后的TextBox

    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
    
        5
  •  -1
  •   user3410566    10 年前

    试试这个:

         <Trigger Property="IsHighlighted" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="OrangeRed"/>
                                <Setter Property="Foreground" Value="White"/>
                            </Trigger>