WPF实现一个搜索按钮的效果
程序员文章站
2022-05-29 20:05:20
...
效果图如下:
代码如下:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Horizontal" Height="24">
<Border CornerRadius="5,0,0,5" BorderThickness="1,1,0,1" BorderBrush="#FF737070">
<TextBox Name="textBox1" Width="150" Background="Transparent" BorderThickness="0" FontSize="22" />
</Border>
<Button Tag="Images/search.png" Name="btnSearch" Width="25">
<Button.Template>
<ControlTemplate>
<Border CornerRadius="0,5,5,0" Background="Transparent" BorderThickness="0,1,1,1" BorderBrush="#FF737070">
<Image Name="imgSearch" Width="22" Height="22" Source="{Binding Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Window>
自己画的64*64的图标search.png如下:
运行时,在工程文件夹下新建Image文件夹,然后把search.png放到该文件夹下