开发者

WP7 Textbox with title inside of textbox

I would like to have a textbox that lets a user enter some text (obviously). Let's say it's 'Title'. Is there a pre-built control that shows the name of the field (Title in this case) inside of the text box and then have it clear o开发者_开发技巧ut when the user enter the field. Example: The search box at the top of this page has 'Search' but when you enter the box it goes away.


Watermarked TextBox

I think I remember it also being in the Mango Silverlight Toolkit too, correct me if I'm wrong: Mango Silverlight Toolkit


This is an example. Put the GotFocus and LostFocus event in your textbox(in .xaml page).

<TextBox x:Name="UrlTextBox" Text="Search" Margin="0,0,98,0" GotFocus="UrlTextBox_GotFocus" LostFocus="UrlTextBox_LostFocus"/>       

In xaml.cs page, add the following codes-

    private void UrlTextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        if (UrlTextBox.Text == "Search")
        {
            UrlTextBox.Text = "";
            SolidColorBrush Brush1 = new SolidColorBrush();
            Brush1.Color = Colors.Gray;
            UrlTextBox.Foreground = Brush1;
        }

        else
        {
            char[] strDataAsChars = UrlTextBox.Text.ToCharArray();
            int i = 0;
            for (i = UrlTextBox.SelectionStart - 1; ((i >= 0) &&
                               (strDataAsChars[i] != ' ')); --i) ;
            int selBegin = i + 1;
            for (i = UrlTextBox.SelectionStart; ((i < strDataAsChars.Length) &&
                                              (strDataAsChars[i] != ' ')); ++i) ;
            int selEnd = i;
            UrlTextBox.Select(selBegin, selEnd - selBegin);
        }
    }

    private void UrlTextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        if (UrlTextBox.Text == String.Empty)
        {
            UrlTextBox.Text = "Search";
            SolidColorBrush Brush2 = new SolidColorBrush();
            Brush2.Color = Colors.Gray;
            UrlTextBox.Foreground = Brush2;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜