我该如何更改TextBox中高亮文本的颜色?

6

WPF使用系统强调颜色来绘制选定文本的背景。我也想覆盖它。

我有一个textBox的控件模板:

<ControlTemplate TargetType="TextBox">
    <Border Name="Border"
          CornerRadius="2" 
          Padding="2"
          Background="Transparent"
          BorderThickness="0" >
        <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource TextBoxDisabledBackgroundColor}"/>
            <Setter Property="Foreground" Value="{StaticResource TextBoxDisabledForegroundColor}"/>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="false">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource TextBoxBackgroundColor}"/>
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

如何更改此模板以覆盖突出显示的文本和背景颜色?

2个回答

8
在.NET 4中,您可以使用文本框的SelectionBrush属性。早期版本需要您在代码中覆盖系统颜色,因为没有易于公开的属性 - 文本框将只使用系统定义的值。

谢谢!我使用SelectionBrush来更改所选文本的背景颜色。但我还需要更改所选文本的颜色。有没有可能做到这一点? - Diana
有没有一种方法可以在Windows Forms中实现这个?(不使用WPF TextBox) - Momoro
@Momoro,您应该发布一个单独的问题来询问这个。 - Dan Puzey
资源字典中无法工作... - Toni

-2

我用样式来实现,例如:

  <Style x:Key="BoundedTextBox"  TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsAutoCalculated}" Value="True">
            <Setter Property="Background" Value="{StaticResource MyBlue}" />
        </Trigger>
    </Style.Triggers>
</Style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接