2011-09-14 126 views
2

WPF使用系統高亮顏色繪製選定文本的背景。我也想重寫它。如何更改TextBox的突出顯示的文本顏色?

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

我怎樣才能改變這個模板覆蓋高亮顯示的文本和背景顏色?

回答

5

在.NET 4中,您可以使用文本框的SelectionBrush屬性。

早期版本要求您覆蓋系統顏色代碼中,因爲沒有容易暴露的屬性 - 文本框只會使用系統定義的值。

+0

謝謝!我使用SelectionBrush來更改所選文本的背景顏色。但我也需要改變選定的文字顏色。有沒有這樣做的可能性? – Diana

+0

您不能:http://stackoverflow.com/a/10850718/37168 – stone

-1

我的風格做了,例如:

<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> 
相關問題