WPF Custom Search box

Comes the search box UI ugly Let us transform yourself some bar

Search Box design process is relatively simple:

1, first define a Rectangle as a background

2, then the middle put TextBox input, which can override the template. Prompt on the Label templates, you can hide the display control trigger template ~

3. Search button - we casually in the next Internet on the line.

UserControl interface:

<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.ColumnDefinitions>

</Grid.ColumnDefinitions>

<TextBox.Template>

<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”>

<ControlTemplate.Triggers>

</ControlTemplate.Triggers>

</TextBox.Template>

<Button.Template>

<ControlTemplate.Triggers>

</ControlTemplate.Triggers>

</Button.Template>

UserControl background:

public partial class SearchControl : UserControl

{

public SearchControl()

{

InitializeComponent();

}

public event EventHandler 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; } 
}

Direct reference on the line: wpfApplication18: SearchControl </ wpfApplication18: SearchControl>

Renderings

Guess you like

Origin blog.csdn.net/weixin_44589117/article/details/92428026