top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Placeholder Textbox Windows Phone 8??

+2 votes
575 views

How to create place holder Textbox in windows phone 8 application

posted Oct 13, 2014 by Sathish Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes

PaceHolder TextBox:

    This technique uses the Background property to show / hide placeholder textbox.
    Advantage is that Placeholder is shown event when Textbox has the focus

How it works:

              When textbox has a value, background set to White to cover up PlaceHolder text.
              When textbox is empty, background becomes Transparent to show PlaceHolder text.

Design Code:

          <Grid>
             <Grid.Resources>
            <ux:NotEmptyConverter x:Key="NotEmptyConverter" />

           <Style TargetType="{x:Type Control}" x:Key="DefaultStyle">
            <Setter Property="FontSize" Value="20" />
            <Setter Property="Margin" Value="10"/>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
            <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
        </Style>

        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultStyle}"></Style>

    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" Text="Placeholder Text Is Here" Foreground="DarkGray" />
    <TextBox Grid.Row="0" Name="TextBoxEdit" 
            Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultStyle}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=FirstName.Length, FallbackValue=0, TargetNullValue=0}" Value="0">
                        <Setter Property="Background" Value="Transparent"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=FirstName, FallbackValue=0, TargetNullValue=0, Converter={StaticResource NotEmptyConverter}}" Value="false">
                        <Setter Property="Background" Value="White"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</Grid>

// Corresponding value converter to detect non-empty string

public class NotEmptyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = value as string;
        return string.IsNullOrEmpty(s);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
answer Oct 15, 2014 by Jdk
Similar Questions
+1 vote

The windows phone emulator wasn't able to ensure the virtual machine was running
Something happened while starting a virtual machine: Emulator WVGA 512MB. Could not initilize.
Not enough memory in the system to start virtual machine Emulator WVGA 512.MB XXXXXXX with ram size 512 megabytes

Any give me a appropriate solution to resolve this.

...