简体   繁体   中英

Placeholder in textbox in windows phone 7 application

I am building an app for windows phone 7. I have a form with a few TextBoxes. Now I want to put a placeholder in my form which I have created programmatically. In some cases it works fine like when I click the TextBox the placeholder is cleared. But it works like a value. The text box is not considered to contain Null value. Please have a look at my code:

Xaml:

<TextBox GotFocus="OnGotFocus" Canvas.Left="6" Canvas.Top="6" Height="74" Name="name" Text="*Name" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus1" Canvas.Left="6" Canvas.Top="66" Height="74" Name="age" Text="*Age" Width="453" BorderThickness="0" />
            <TextBlock Canvas.Left="20" Canvas.Top="157" Height="44" Name="gen" Text="Gender" Foreground="Black" FontFamily="Verdana" FontSize="24" Width="134" />
            <RadioButton Canvas.Left="139" Canvas.Top="157" FontStyle="Italic" Foreground="Black" Content="Male" Height="71" Name="male" Width="154" />
            <RadioButton Canvas.Left="139" Canvas.Top="207"  FontStyle="Italic" Foreground="Black" Content="Female" Height="71" Name="fem" Width="140" />
            <TextBox GotFocus="OnGotFocus2" Canvas.Left="6" Canvas.Top="267" Height="74" Name="sadd" Text="*Street Address" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus3" Canvas.Left="6" Canvas.Top="327" Height="74" Name="cadd" Text="*City Address" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus4" Canvas.Left="6" Canvas.Top="387" Height="74" Name="eadd" Text="*Email Address" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus5" Canvas.Left="6" Canvas.Top="447" Height="74" Name="phn" Text="*Phone" Width="453" BorderThickness="0"/>
            <TextBox GotFocus="OnGotFocus6" Canvas.Left="6" Canvas.Top="507" Height="74" Name="zip" Text="*Zip Code" Width="453" BorderThickness="0"/>

Cs file:

private void OnGotFocus(object sender, RoutedEventArgs e)
    {
        if (name.Text.Equals("*Name", StringComparison.OrdinalIgnoreCase))
        {
            name.Text = string.Empty;
        }
    }

    private void OnGotFocus1(object sender, RoutedEventArgs e)
    {
        if (age.Text.Equals("*Age", StringComparison.OrdinalIgnoreCase))
        {
            age.Text = string.Empty;
        }
    }

    private void OnGotFocus2(object sender, RoutedEventArgs e)
    {
        if (sadd.Text.Equals("*Street Address", StringComparison.OrdinalIgnoreCase))
        {
            sadd.Text = string.Empty;
        }
    }

    private void OnGotFocus3(object sender, RoutedEventArgs e)
    {
        if (cadd.Text.Equals("*City Address", StringComparison.OrdinalIgnoreCase))
        {
            cadd.Text = string.Empty;
        }
    }

    private void OnGotFocus4(object sender, RoutedEventArgs e)
    {
        if (eadd.Text.Equals("*Email Address", StringComparison.OrdinalIgnoreCase))
        {
            eadd.Text = string.Empty;
        }
    }

    private void OnGotFocus5(object sender, RoutedEventArgs e)
    {
        if (phn.Text.Equals("Phone", StringComparison.OrdinalIgnoreCase))
        {
            phn.Text = string.Empty;
        }
    }

    private void OnGotFocus6(object sender, RoutedEventArgs e)
    {
        if (zip.Text.Equals("*Zip Code", StringComparison.OrdinalIgnoreCase))
        {
            zip.Text = string.Empty;
        }
    }

I want the placeholder not to be considered as a value. Please help.

have you looked at windows toolkit's PhonetextBox , it has a hint property. It should help you.

Also you could use a watermarked textbox control supplied by codeplex. It would work according to your needs

here is the link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM