2015-02-07 89 views
0

我正在爲Windows Phone 8創建應用程序,我需要一個搜索框。帶按鈕的Windows Phone TextBox

在理論,我想這一點:

enter image description here

當用戶寫他想要搜索的內容。

儘管我想在最後有一個按鈕(用X表示),當用戶單擊它時,它會刪除所有文本。此外,此按鈕只應在出現文字或與默認文字不同時出現。

實際的問題,如果我有(圖片)是,當我集中文本框,按鈕消失。

我該怎麼辦?看過幾個網站,但不能做我想要的。

編輯: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" /> 
+0

請發表您現有的XAML和代碼。 – 2015-02-07 03:02:32

+0

爲什麼不把按鈕放在文本框的外側,將其縮放到相同垂直尺寸的正方形並將其摺疊。然後爲TextChanged添加一個事件處理程序,用於在有人更改文本時檢查該框的內容。如果它不是默認的而不是空的,那麼將該按鈕設置爲可見? – redwizard000 2015-02-07 03:10:15

+0

添加了XAML @PeterTorr。它的工作原理是紅色的,但我想把它放在設計目的裏面。 – Cafn 2015-02-07 03:32:04

回答

1

您必須使用Textbox GotFocus事件和LostFocus事件。它看起來像Google搜索框。它一定會幫助你。按鈕的所有下載圖像的 首先從here

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:

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"; 
    } 
+0

在按鈕中使用圖像時出現問題,並且此答案是最好的。謝謝! – Cafn 2015-02-08 02:07:59

2

默認情況下,當你在它挖掘文本框會變成白色。它與您的「X」按鈕顏色相同。將按鈕的顏色更改爲其他顏色。

將Foreground =「Black」添加到您的按鈕的XAML中,或從顏色選擇器中選取一種顏色。