3

I'm using the following style on a Textbox so that it has text and a background colour until someone tries to enter data into it. Works fine but my problem arises because it is a login screen and my other control is a Passwordbox which won't let me access the Password property (which is the equivalent to the Text property of the Textbox). Any advice on how I would work around this?

<Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <Style.Resources>
                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="Search" Foreground="LightGray"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <Trigger Property="Text" Value="{x:Null}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="Background" Value="White" />
            </Trigger>
            </Style.Triggers>
            <Setter Property="Control.Foreground" Value="#4C2C66"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
windowsgm
  • 1,566
  • 4
  • 23
  • 55

1 Answers1

4

If you are using a PasswordBox, make sure to change the TargetType and use a DataTrigger:

<Style TargetType="{x:Type PasswordBox}" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <Style.Resources>
                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="Search" Foreground="LightGray"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Password}" Value="{x:Null}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </DataTrigger>

                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="Background" Value="White" />
                </Trigger>
            </Style.Triggers>
            <Setter Property="Control.Foreground" Value="#4C2C66"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
       </Style>
Dom
  • 38,906
  • 12
  • 52
  • 81
  • 2
    Granted this changes the colour if the textbox is empty... But it stays that colour even if something is typed. What if you want to change the colour if something is entered? – Offer Jun 03 '14 at 11:02
  • You may want to use a converter in that case. https://stackoverflow.com/a/356690/3990948 – Sajith Sageer Jan 22 '19 at 06:41