WPF Custom Search box

Source: Internet
Author: User

Search icon in the control: http://www.easyicon.net/1183666-Search_icon.html

The search box design process is relatively simple:

1, first define a rectangle as the background

2, then the middle of the textbox input, you can rewrite the template. The prompt label is placed in the template, and you can control the hidden display in the trigger of the template ~

3, Search button-everyone on the Internet next on the line.

UserControl Interface:

<UserControlx: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= "+"MinWidth= "Max"Background= "Transparent"D:designheight= "+"D:designwidth= "Max">    <Grid>        <grid.columndefinitions>            <ColumnDefinitionWidth= " the"></ColumnDefinition>            <ColumnDefinitionWidth="*"></ColumnDefinition>            <ColumnDefinitionWidth= "The "></ColumnDefinition>        </grid.columndefinitions>        <RectangleGrid.column= "0"Grid.columnspan= "3"Fill= "Lightgray"RadiusX= " the"RadiusY= " the"Opacity= "0.8"></Rectangle>                <TextBoxx:name= "Tbxinput"Grid.column= "1"KeyDown= "Tbxinput_onkeydown">            <textbox.template>                <ControlTemplateTargetType= "TextBox">                    <Grid>                        <TextBlockx:name= "Uc_tblshow"Text= "Please enter ..."Foreground= "Gray"Opacity= "0.5"VerticalAlignment= "Center"Visibility= "Collapsed"></TextBlock>                        <TextBoxx:name= "Uc_tbxcontent"Foreground= "Gray"Background= "Transparent"VerticalAlignment= "Center"verticalcontentalignment= "Center"borderthickness= "0"Text="{Binding Elementname=tbxinput,path=text,mode=twoway}"FontSize= "+"></TextBox>                    </Grid>                    <controltemplate.triggers>                        <TriggerSourceName= "Uc_tbxcontent" Property= "Text"Value="">                            <SetterTargetName= "Uc_tblshow" Property= "Visibility"Value= "Visible"></Setter>                        </Trigger>                        <TriggerSourceName= "Uc_tbxcontent" Property= "IsFocused"Value= "True">                            <SetterTargetName= "Uc_tblshow" Property= "Visibility"Value= "Collapsed"></Setter>                        </Trigger>                    </controltemplate.triggers>                </ControlTemplate>            </textbox.template>        </TextBox>                <Buttonx:name= "Btnsearch"Grid.column= "2"Click= "Btnsearch_onclick"Cursor= "Hand">            <button.template>                <ControlTemplateTargetType= "button">                    <Grid>                        <Imagex:name= "Uc_image"Source= "1181298.png"Height= " the"Width= " the"></Image>                        <ContentPresenter></ContentPresenter>                    </Grid>                    <controltemplate.triggers>                        <Trigger Property= "IsMouseOver"Value= "true">                            <SetterTargetName= "Uc_image" Property= "Height"Value= "+"></Setter>                            <SetterTargetName= "Uc_image" Property= "Width"Value= "+"></Setter>                        </Trigger>                    </controltemplate.triggers>                </ControlTemplate>            </button.template>        </Button>    </Grid></UserControl>
View Code

UserControl Backstage:

     Public Partial classSearchcontrol:usercontrol { PublicSearchControl () {InitializeComponent (); }         Public EventEventhandler<searcheventargs>Onsearch; Private voidBtnsearch_onclick (Objectsender, RoutedEventArgs e)        {Execcutesearch (); }        Private voidTbxinput_onkeydown (Objectsender, KeyEventArgs e)        {Execcutesearch (); }        Private voidExeccutesearch () {if(onsearch!=NULL)            {                varargs=NewSearcheventargs (); Args. SearchText=Tbxinput.text; Onsearch ( This, args); }        }    }     Public classSearcheventargs:eventargs { Public stringSearchText {Get;Set; } }
View Code

Just a direct quote,:<wpfapplication18:searchcontrol></wpfapplication18:searchcontrol>.

WPF Custom Search box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.