开发者

How can I change the highlighted text color for a TextBox?

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 a开发者_JAVA技巧nd background color?


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜