Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP TextBox Background when Focused

Tags:

c#

xaml

uwp

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.

like image 256
D12.Erica Avatar asked Sep 08 '17 06:09

D12.Erica


2 Answers

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>
like image 199
Mapplesoft Avatar answered Nov 01 '22 03:11

Mapplesoft


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.

like image 35
Phil Wright Avatar answered Nov 01 '22 03:11

Phil Wright