您必須使用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";
}
請發表您現有的XAML和代碼。 – 2015-02-07 03:02:32
爲什麼不把按鈕放在文本框的外側,將其縮放到相同垂直尺寸的正方形並將其摺疊。然後爲TextChanged添加一個事件處理程序,用於在有人更改文本時檢查該框的內容。如果它不是默認的而不是空的,那麼將該按鈕設置爲可見? – redwizard000 2015-02-07 03:10:15
添加了XAML @PeterTorr。它的工作原理是紅色的,但我想把它放在設計目的裏面。 – Cafn 2015-02-07 03:32:04