DanielBkr
DanielBkr

Reputation: 93

Xamarin UWP Set PasswordBox Placeholder Color

I have a Xamarin.Forms Shared project with a login page. For UWP I would like to use the password reveal functionality, which is why I need the PasswordBox Control. I would like to set the placeholdertext color for the PasswordBox control in UWP. The problem is Xamarin does not have a PasswordBoxRenderer and the PasswordBox control does not have a property to edit the placeholder foreground color.

I haven't been able to find an updated/Xamarin solution. Any help or solution is appreciated.

Upvotes: 1

Views: 442

Answers (1)

Denis Kosov
Denis Kosov

Reputation: 697

The best way to override default brush in PasswordBox resource, like this:

<PasswordBox PlaceholderText="PASSWORD">
            <PasswordBox.Resources>
                <SolidColorBrush x:Key="TextControlPlaceholderForegroundFocused" Color="Red"/>
                <SolidColorBrush x:Key="TextControlPlaceholderForeground" Color="Red"/>
            </PasswordBox.Resources>
        </PasswordBox>

You also may override: TextControlPlaceholderForegroundPointerOver, TextControlPlaceholderForegroundDisabled

Add new UserControl to you project then change it to:

<PasswordBox
    x:Class="App3.CustomPasswordBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Width="200" Height="50" PlaceholderText="PlaceHolder">
    <PasswordBox.Resources>
        <SolidColorBrush x:Key="TextControlPlaceholderForegroundFocused" Color="Red"/>
        <SolidColorBrush x:Key="TextControlPlaceholderForeground" Color="Red"/>
    </PasswordBox.Resources>
</PasswordBox>

Remove CustomPasswordBox.xaml.cs.

Create new Renderer:

public class PasswordRenderer : ViewRenderer<Entry, CustomPasswordBox>
    {
        
    }

Upvotes: 0

Related Questions