Windows Phone 带按钮的文本框
Windows Phone TextBox with a button
我正在为 windows phone 8 创建一个应用程序,我需要一个搜索框。
理论上,我想要这个:
用户写他想搜索的地方。
虽然我想在末尾有一个按钮(由 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" />
默认情况下,当您点击文本框时,它会变成白色。它与 "X" 按钮的颜色相同。将按钮的颜色更改为其他颜色。
将 Foreground="Black" 添加到按钮的 XAML 或从颜色选择器中选择颜色。
您必须使用文本框 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";
}
我正在为 windows phone 8 创建一个应用程序,我需要一个搜索框。
理论上,我想要这个:
用户写他想搜索的地方。
虽然我想在末尾有一个按钮(由 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" />
默认情况下,当您点击文本框时,它会变成白色。它与 "X" 按钮的颜色相同。将按钮的颜色更改为其他颜色。
将 Foreground="Black" 添加到按钮的 XAML 或从颜色选择器中选择颜色。
您必须使用文本框 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";
}