How can I change the highlighted text color for a

2019-05-23 06:01发布

WPF uses the system highlight color for painting the background of the selected text. I'd like to override it too.

I have a control template for 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>

How can I change this template to override the highlighted text and background color?

2条回答
劫难
2楼-- · 2019-05-23 06:35

I did it with styles, e.g.:

  <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>
查看更多
在下西门庆
3楼-- · 2019-05-23 06:40

In .NET 4 you can use the SelectionBrush property of the textbox.

Earlier versions require you to override system colours in-code, as there was no easily-exposed property for this - the textbox would just use the system-defined values.

查看更多
登录 后发表回答