简体   繁体   English

Windows Phone文本框(带按钮)

[英]Windows Phone TextBox with a button

I'm creating an application for windows phone 8 and I need a search box. 我正在为Windows Phone 8创建一个应用程序,并且需要一个搜索框。

On theory, I want this : 从理论上讲,我想要这样:

在此处输入图片说明

Where the user writes what he wants to search. 用户在其中写他想搜索的内容。

Though I want to have a button at the end (represented by the X) that when the user click it, it deletes all text. 尽管我想在结尾处有一个按钮(用X表示),当用户单击它时,它会删除所有文本。 Also this button should only appear when there is text or it is different from the default text. 同样,该按钮仅在有文本或与默认文本不同时才出现。

The actual problem if what I have (the picture) is that when I focus the textbox, the button disapears. 如果我拥有(图片)的实际问题是当我聚焦文本框时,该按钮消失了。

How can I do it? 我该怎么做? Seen several websites, but cant make what I want. 看过几个网站,但不能满足我的需求。

EDIT : XAML 编辑:XAML

<TextBox VerticalAlignment="Stretch"  HorizontalAlignment="Stretch" Grid.Row="0" Text="find" />
<Button Content="X" Width="40" Height="40" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" Grid.Row="0" />

By default the text box will turn white when you tap on it. 默认情况下,当您点击该文本框时,它会变成白色。 It is the same color as your "X" button. 它与您的“ X”按钮的颜色相同。 Change the color of the button to something else. 将按钮的颜色更改为其他颜色。

Add Foreground="Black" to your XAML for your button or pick a color from the color picker. 为您的按钮将Foreground =“ Black”添加到XAML或从颜色选择器中选择一种颜色。

You have to work with Textbox GotFocus event and LostFocus event. 您必须使用Textbox GotFocus事件和LostFocus事件。 It looks like Google search box. 看起来像Google搜索框。 It will surely help you. 它一定会为您提供帮助。 First of all download image for the button from here 首先从这里下载按钮的图像

XAML: XAML:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,531">
            <TextBox Name="txtSearch" 
                     Text="Search"
                     GotFocus="txtSearch_GotFocus"
                     LostFocus="txtSearch_LostFocus"
                     VerticalAlignment="Top"
                     Foreground="Gray"/>

              <Button 
                    Click="Button_Click"
                    Width="50" 
                    Height="60" 
                    BorderBrush="Transparent"
                     HorizontalAlignment="Right" 
                     VerticalAlignment="Stretch"
                    Margin="10" Grid.Row="0">
                <Button.Background>
                    <ImageBrush Stretch="Uniform"  ImageSource="/box_drawings_light_diagonal_cross_u2573_icon_256x256.png" />
                </Button.Background>
            </Button>
           </Grid>

XAML.CS: XAML.CS:

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

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

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        txtSearch.Text = "Search";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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