How To Change Text-Box Placeholder Color🎨 In UWP Project

Introduction

In this article, we’ll learn how we can change the textbox placeholder color in UWP applications. In UWP applications, the default placeholder color is black, if our application textbox background color is also black then the placeholder text will have the same color, so in this article we will change the placeholder color. In this way, we can create our application better. I hope you will like it.

Create your UWP Application

Step 1

Open Visual Studio and select File >> New Project.
 
UWP
 
The "New Project" window will pop up. Select "Blank App (Universal Windows)", name your project, and click “OK”.

UWP
 
 A "New Universal Windows Project" window will pop up. Select the target version and minimum version and click “OK”.

UWP

Step 2

Once your project is ready, open Solution Explorer. Click on “MainPage.xaml”, and design the application according to the requirement.

UWP

Step 3

Now, copy the sample code and paste before "ScrollViewer".

In the style tag, there is a key. Its name should be unique from other and can be user-defined.
  1. <Page.Resources>  
  2.   
  3.         <Style x:Key="vikasTextboxStyle" TargetType="TextBox">  
  4.             <Setter Property="Template">  
  5.                 <Setter.Value>  
  6.                     <ControlTemplate TargetType="TextBox">  
  7.   
  8.                         <ContentControl x:Name="PlaceholderTextContentPresenter"  
  9.                               Grid.Row="1"    
  10.                               Margin="{TemplateBinding BorderThickness}"  
  11.                               Padding="{TemplateBinding Padding}"  
  12.                               IsTabStop="False"  
  13.                               Grid.ColumnSpan="2"  
  14.                               Content="{TemplateBinding PlaceholderText}"   
  15.                               IsHitTestVisible="False"/>  
  16.                     </ControlTemplate>  
  17.                 </Setter.Value>  
  18.             </Setter>  
  19.         </Style>  
  20.     </Page.Resources>  

And add the key in Style but remember that the key name should be same as you given in page.resourse style tag and write the code in textbox as given in the below screenshot.

  1. <Style="{StaticResource vikasTextboxStyle}">  
  2.   
  3. <TextBox.Resources>    
  4.                         <Style TargetType="ContentControl">    
  5.                             <Setter Property="Foreground" Value="Green"/>    
  6.                         </Style>    
  7.  </TextBox.Resources>    

For a complete sample, you can use the code below.

  1. <Page  
  2.     x:Class="AppPackages.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:AppPackages"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <Page.Resources>  
  11.         <Style x:Key="vikasTextboxStyle" TargetType="TextBox">  
  12.             <Setter Property="Template">  
  13.                 <Setter.Value>  
  14.                     <ControlTemplate TargetType="TextBox">  
  15.   
  16.                         <ContentControl x:Name="PlaceholderTextContentPresenter"  
  17.                               Grid.Row="1"    
  18.                               Margin="{TemplateBinding BorderThickness}"  
  19.                               Padding="{TemplateBinding Padding}"  
  20.                               IsTabStop="False"  
  21.                               Grid.ColumnSpan="2"  
  22.                               Content="{TemplateBinding PlaceholderText}"   
  23.                               IsHitTestVisible="False"/>  
  24.                     </ControlTemplate>  
  25.                 </Setter.Value>  
  26.             </Setter>  
  27.         </Style>  
  28.     </Page.Resources>  
  29.   
  30.     <ScrollViewer>  
  31.         <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  32.             <Grid.RowDefinitions>  
  33.                 <RowDefinition MaxHeight="150" MinHeight="140"></RowDefinition>  
  34.                 <RowDefinition MaxHeight="500" MinHeight="400"></RowDefinition>  
  35.             </Grid.RowDefinitions>  
  36.             <Border>  
  37.                 <StackPanel>  
  38.                     <Rectangle MinWidth="430" MinHeight="175">  
  39.                         <Rectangle.Fill>  
  40.                             <LinearGradientBrush StartPoint="0,0" EndPoint="0.8,1.3">  
  41.                                 <GradientStop Color="Brown" Offset="1"  />  
  42.                                 <GradientStop Color="CornflowerBlue" Offset=".4" />  
  43.                             </LinearGradientBrush>  
  44.                         </Rectangle.Fill>  
  45.                     </Rectangle>  
  46.                 </StackPanel>  
  47.             </Border>  
  48.             <StackPanel Grid.Row="1" HorizontalAlignment="Center" Margin="0,180,0,150" >  
  49.   
  50.                 <TextBox x:Name="Email" PlaceholderText="Type Email..." Margin="10,30,10,20" Style="{StaticResource vikasTextboxStyle}">  
  51.                     <TextBox.Resources>  
  52.                         <Style TargetType="ContentControl">  
  53.                             <Setter Property="Foreground" Value="Green"/>  
  54.                         </Style>  
  55.                     </TextBox.Resources>  
  56.                 </TextBox>  
  57.   
  58.                 <TextBox x:Name="Password" PlaceholderText="Type Password..."  Margin="10,10,10,10" Style="{StaticResource vikasTextboxStyle}">  
  59.                     <TextBox.Resources>  
  60.                         <Style TargetType="ContentControl">  
  61.                             <Setter Property="Foreground" Value="Red"/>  
  62.                         </Style>  
  63.                     </TextBox.Resources>  
  64.                 </TextBox>  
  65.                   
  66.                 <Button Content="Welcome to UWP application" HorizontalAlignment="Stretch"  
  67.                Width="367" Foreground="White" Background="OrangeRed" Height="44" Margin="10,10,10,30"></Button>  
  68.             </StackPanel>  
  69.         </Grid>  
  70.     </ScrollViewer>  
  71. </Page>  

UWP

 

Now, you can see in the screenshot the email placeholder color is green and the password color is red. You can set the color as per your app requirement.


Similar Articles