For some reason there's no easy way to change focused background of TextBox from the default White color.
The only way it works (I need it to be dark-ish or transparent) is to create custom textbox, paste bazzillion lines of code (from here) and then edit TWO lines:
<VisualState x:Name="Focused">
<Storyboard>
...
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="#000000" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement" Soryboard.TargetProperty="Opacity">
<DiscreteObjectKeyFrame KeyTime="0" Value="0.1" />
</ObjectAnimationUsingKeyFrames>
My question is: is there a better way to do it? Is all the other code necessary (~240 lines)? Thank you.
Do this in your App.xaml file:
<Application>
<Application.Resources>
<SolidColorBrush x:Key="TextControlBackgroundFocused" Color="Black" Opacity="0.2"/>
<SolidColorBrush x:Key="TextControlForegroundFocused" Color="White"/>
<SolidColorBrush x:Key="TextControlBorderBrushFocused" Color="White" Opacity="0.2"/>
</Application.Resources>
</Application>
This will overwrite the default colors with your own custom colors for every TextBox
in your project. If you want to apply the appearance to only some of your TextBoxes
, define it locally for each TextBox
:
<TextBox>
<TextBox.Resources>
Put brushes here
</TextBox.Resources>
</TextBox>
It would be easier to create a style and then apply it. At design time you can use the Document Outline pane in Visual Studio and right-click the TextBox. Then choose the Edit Template -> Edit Copy. Then modify that style in the same way you have done in your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With