欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF实现一个搜索按钮的效果

程序员文章站 2022-05-29 20:05:20
...

效果图如下:WPF实现一个搜索按钮的效果

代码如下:

<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如下:
WPF实现一个搜索按钮的效果
运行时,在工程文件夹下新建Image文件夹,然后把search.png放到该文件夹下