首页 > 编程 > .NET > 正文

详解WPF自定义搜索框代码

2024-07-10 12:54:19
字体:
来源:转载
供稿:网友

网页开发在互联网上运行很广泛,其实WPF是制作界面的一大利器,下面就由错新技术频道小编和大家一起看一看详解WPF自定义搜索框代码,希望对你学习这方面知识有所帮助。

首先下载搜索图标:

搜索框设计过程比较简单: 

1、先定义一个Rectangle作为背景 

2、然后中间放TextBox输入,可以重写其中的模板。提示语Label放在模板中,可以在模板的触发器中控制隐藏显示~ 

3、搜索按钮-大家随便在网上下个就行了。 

UserControl界面: 

<UserControl x:Class="WpfApplication18.SearchControl"       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        mc:Ignorable="d" MinHeight="30" MinWidth="150" Background="Transparent"       d:DesignHeight="30" d:DesignWidth="150">  <Grid>    <Grid.ColumnDefinitions>      <ColumnDefinition Width="15"></ColumnDefinition>      <ColumnDefinition Width="*"></ColumnDefinition>      <ColumnDefinition Width="36"></ColumnDefinition>    </Grid.ColumnDefinitions>    <Rectangle Grid.Column="0" Grid.ColumnSpan="3" Fill="LightGray" RadiusX="15" RadiusY="15" Opacity="0.8"></Rectangle>        <TextBox x:Name="TbxInput" Grid.Column="1" KeyDown="TbxInput_OnKeyDown">      <TextBox.Template>        <ControlTemplate TargetType="TextBox">          <Grid>            <TextBlock x:Name="Uc_TblShow" Text="请输入..." Foreground="Gray" Opacity="0.5" VerticalAlignment="Center" Visibility="Collapsed"></TextBlock>            <TextBox x:Name="Uc_TbxContent" Foreground="Gray" Background="Transparent" VerticalAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0"                 Text="{Binding ElementName=TbxInput,Path=Text,Mode=TwoWay}" FontSize="18"></TextBox>          </Grid>          <ControlTemplate.Triggers>            <Trigger SourceName="Uc_TbxContent" Property="Text" Value="">              <Setter TargetName="Uc_TblShow" Property="Visibility" Value="Visible"></Setter>            </Trigger>            <Trigger SourceName="Uc_TbxContent" Property="IsFocused" Value="True">              <Setter TargetName="Uc_TblShow" Property="Visibility" Value="Collapsed"></Setter>            </Trigger>          </ControlTemplate.Triggers>        </ControlTemplate>      </TextBox.Template>    </TextBox>        <Button x:Name="BtnSearch" Grid.Column="2" Click="BtnSearch_OnClick" Cursor="Hand">      <Button.Template>        <ControlTemplate TargetType="Button">          <Grid>            <Image x:Name="Uc_Image" Source="1181298.png" Height="20" Width="20"></Image>            <ContentPresenter></ContentPresenter>          </Grid>          <ControlTemplate.Triggers>            <Trigger Property="IsMouseOver" Value="true">              <Setter TargetName="Uc_Image" Property="Height" Value="25"></Setter>              <Setter TargetName="Uc_Image" Property="Width" Value="25"></Setter>            </Trigger>          </ControlTemplate.Triggers>        </ControlTemplate>      </Button.Template>    </Button>  </Grid></UserControl> 

UserControl后台:

  public partial class SearchControl : UserControl  {    public SearchControl()    {      InitializeComponent();    }    public event EventHandler<SearchEventArgs> OnSearch;     private void BtnSearch_OnClick(object sender, RoutedEventArgs e)    {      ExeccuteSearch();    }    private void TbxInput_OnKeyDown(object sender, KeyEventArgs e)    {      ExeccuteSearch();    }    private void ExeccuteSearch()    {      if (OnSearch!=null)      {        var args=new SearchEventArgs();        args.SearchText = TbxInput.Text;        OnSearch(this, args);      }    }  }  public class SearchEventArgs : EventArgs  {    public string SearchText { get; set; }  } 

相信大家对详解WPF自定义搜索框代码都有了一定的了解,如果你想了解更多的专业知识,那就关注js.VeVb.com吧!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表