WPF TextBoxの強調表示されたテキストの色を変更するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/405967

質問

WPF TextBox は、選択されたテキストの背景を描画するためにシステムハイライトカラーをネイティブに使用します。 OS /ユーザーのテーマによって異なるため、これをオーバーライドして一貫性を持たせたいと思います。

ListBoxItem の場合、ニートトリック(以下を参照)では、 HighlightBrushKey を使用して、焦点を絞った設定でシステムハイライトの色をカスタマイズします。

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

残念ながら TextBox でも同じトリックは機能しません。 ControlTemplate をオーバーライドする以外に、他のアイデアはありますか?

提案をありがとう!

注:これWPF 4に動作が追加されたようです。

役に立ちましたか?

解決

スティーブが言及したように:注:この動作はWPF 4に追加されているようです。

同じ問題にぶつかりました。

WPF博士の言うとおり

  

&quot;それは完全に不可能です   現在の.NETリリース(3.0&amp; 3.5   ベータ)。コントロールは   システム設定を使用します...   コントロールテンプレートをすべて見てください。&quot;

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

他のヒント

.NET 4以降、 < code> TextBoxBase.SelectionBrush

eg

<TextBox SelectionBrush="Red" SelectionOpacity="0.5"
         Foreground="Blue" CaretBrush="Blue">  

これは、アプリの各 TextBox SelectionBrush をカスタマイズするためのWindows 8.1 .Net 4.6.1テスト済みソリューションです。

/// 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 を含める場合は、型名 TextBox TextBoxBase で4回置き換えます。

TextBoxのスタイルを作成し、背景のSetterを作成できます。 TextBoxスタイルは、ビジュアルツリーの下にあるTextBoxが変更されたTextBoxを取得できるように、デフォルトのスタイルにする必要があります

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">

これを試してください:

     <Trigger Property="IsHighlighted" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="OrangeRed"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top