I found in this post How can I add a hint text to WPF textbox? an inline style I liked to add a hint text to the background of a textbox. I used it to create a style with control template in the <Window.Resources> section Text1:
<Window
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"
Name="Window" Title="MainWindow" Height="120" Width="1000">
<Window.Resources>
<Style x:Key="Text1" TargetType="{x:Type TextBox}" >
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="BorderThickness" Value="2,2,2,2" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Margin" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}" xmlns:sys="clr-namespace:System;assembly=mscorlib" >
<ControlTemplate.Resources>
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<SolidColorBrush x:Key="Whity" Color="White" />
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="This is a hint text" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
<Storyboard x:Key="SrcWrongInput" TargetName="Src" >
<ColorAnimation Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)" To="Red" AutoReverse="True" RepeatBehavior="4x" Duration="0:0:0:0.3"/>
</Storyboard>
</ControlTemplate.Resources>
<Border Name="Border" CornerRadius="2" Padding="2" Background="{StaticResource WindowBackgroundBrush}" BorderBrush="{StaticResource SolidBorderBrush}" BorderThickness="1" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter TargetName="Border" Property="Background" Value="{StaticResource CueBannerBrush}"/>
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter TargetName="Border" Property="Background" Value="{StaticResource CueBannerBrush}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource Whity}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="5*" />
</Grid.RowDefinitions>
<TextBox Name="Src" Grid.Column="0" Grid.Row="0" Style="{StaticResource Text1}" ToolTip="Enter path to exe or folder C:\Program Files\..." VerticalContentAlignment="Center" />
<TextBox Name="Name" Grid.Column="1" Grid.Row="0" Style="{StaticResource Text1}" ToolTip="Enter name" VerticalContentAlignment="Center"/>
<TextBox Name="Rslt" Grid.Column="0" Grid.Row="1" Style="{StaticResource Text1}" ToolTip="Result of procedure" VerticalContentAlignment="Center">
<TextBox.Resources>
<Storyboard x:Key="RsltWrongInput" TargetName="Rslt" >
<ColorAnimation Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)" To="Red" AutoReverse="True" RepeatBehavior="4x" Duration="0:0:0:0.3"/>
</Storyboard>
</TextBox.Resources>
</TextBox>
<Button Name="Do" Grid.Column="2" Grid.Row="0" />
</Grid>
</Grid>
Its loaded with this ps1 script:
Add-Type -AssemblyName presentationframework
[xml]$XAML = Get-Content "MainWindowStack.xaml"
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader"; exit}
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
$DataContextRslt = New-Object System.Collections.ObjectModel.ObservableCollection[Object]
$do.Add_Click({
$rsltWrongInputStory = $Rslt.Resources["RsltWrongInput"]
$rsltWrongInputStory.begin()
})
$Form.ShowDialog() | out-null
It loads and shows the hint text, which disappears as soon as the text is not empty or the textbox gets keyboard focus, which is exactly the behavior I intended. But I am not able to bind the content of the label in the VisualBrush x:Key="CueBannerBrush" to another property of the textbox (e.g. Tag or ToolTip) or to a DataContext to make the hint text adjustable. I tried all kinds of different Binding / TemplateBinding expressions, with relativeSource, ancestors with no result.
I also checked other posts on this topic: In Parameterized style to display hint text in WPF TextBox [duplicate] there is a comment on how and why this could not work followed by this link Adding placeholder text to textbox which has an example for a different approach:
<Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<TextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
Panel.ZIndex="2" />
<TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
It does work, using this style I can set the hint text using the Tag property of the textbox, however it has the restriction that it is not reacting on keyboard focus like the first, the hint text only disappears once a letter is entered.
My attempt to add that behavior with this trigger failed:
<DataTrigger Binding="{Binding Path=IsKeyboardFocused, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
Yet another problem accrued with the first style, when trying to add VerticalContentAlignment="Center" to the setters, the Window does not load anymore, therefore I added it to every textbox individually. There are also two more problems which the execution of the storyboard "SrcWrongInput" which I added to the Sources of the style. For one, I don't know how to address it, inside the style resources, so I also added it to resource of the last textbox named "Rslt" to at least make sure that it is compatible with the style. It is not, I triggered it over the button "Do" click handler in the ps1-script and got this exception:
"Von der BorderBrush-Eigenschaft wird nicht auf "DependencyObject" im Pfad "(BorderBrush).(0)" verwiesen."
The BorderBrush property is not linked to the dependency object at the path (BorderBrush).(0)