Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a sample which demonstrates how to create a watermark textbox in WPF:</p> <pre><code>&lt;Window x:Class="WaterMarkTextBoxDemo.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WaterMarkTextBoxDemo" Height="200" Width="400"&gt; &lt;Window.Resources&gt; &lt;SolidColorBrush x:Key="brushWatermarkBackground" Color="White" /&gt; &lt;SolidColorBrush x:Key="brushWatermarkForeground" Color="LightSteelBlue" /&gt; &lt;SolidColorBrush x:Key="brushWatermarkBorder" Color="Indigo" /&gt; &lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /&gt; &lt;local:TextInputToVisibilityConverter x:Key="TextInputToVisibilityConverter" /&gt; &lt;Style x:Key="EntryFieldStyle" TargetType="Grid" &gt; &lt;Setter Property="HorizontalAlignment" Value="Stretch" /&gt; &lt;Setter Property="VerticalAlignment" Value="Center" /&gt; &lt;Setter Property="Margin" Value="20,0" /&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid Background="LightBlue"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}" Style="{StaticResource EntryFieldStyle}" &gt; &lt;TextBlock Margin="5,2" Text="This prompt dissappears as you type..." Foreground="{StaticResource brushWatermarkForeground}" Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" /&gt; &lt;TextBox Name="txtUserEntry" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" /&gt; &lt;/Grid&gt; &lt;Grid Grid.Row="1" Background="{StaticResource brushWatermarkBackground}" Style="{StaticResource EntryFieldStyle}" &gt; &lt;TextBlock Margin="5,2" Text="This dissappears as the control gets focus..." Foreground="{StaticResource brushWatermarkForeground}" &gt; &lt;TextBlock.Visibility&gt; &lt;MultiBinding Converter="{StaticResource TextInputToVisibilityConverter}"&gt; &lt;Binding ElementName="txtUserEntry2" Path="Text.IsEmpty" /&gt; &lt;Binding ElementName="txtUserEntry2" Path="IsFocused" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Visibility&gt; &lt;/TextBlock&gt; &lt;TextBox Name="txtUserEntry2" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" /&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>TextInputToVisibilityConverter is defined as:</p> <pre><code>using System; using System.Windows.Data; using System.Windows; namespace WaterMarkTextBoxDemo { public class TextInputToVisibilityConverter : IMultiValueConverter { public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture ) { // Always test MultiValueConverter inputs for non-null // (to avoid crash bugs for views in the designer) if (values[0] is bool &amp;&amp; values[1] is bool) { bool hasText = !(bool)values[0]; bool hasFocus = (bool)values[1]; if (hasFocus || hasText) return Visibility.Collapsed; } return Visibility.Visible; } public object[] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture ) { throw new NotImplementedException(); } } } </code></pre> <p><strong>Note:</strong> This is not my code. I found it <a href="http://www.codeproject.com/Articles/26977/A-WatermarkTextBox-in-lines-of-XAML" rel="noreferrer">here</a>, but I think this is the best approach.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload