Xamarin SearchBar Tip 2 – search as you type

See part one on how to pass the text that was typed in passed to the command.

This next tip runs the search command when the text changes, as you type.

Add a new behaviour, I added this to a subfolder “Behaviour”

public class TextChangedBehavior: Behavior<SearchBar>
    protected override void OnAttachedTo(SearchBar bindable)
	bindable.TextChanged += Bindable_TextChanged;

    protected override void OnDetachingFrom(SearchBar bindable)
	bindable.TextChanged -= Bindable_TextChanged;

    private void Bindable_TextChanged(object sender, TextChangedEventArgs e)

Then in the view add a new xmlns


And add the behaviour like so

<SearchBar x:Name="MySearchBar" SearchCommand="{Binding SearchCommand}" SearchCommandParameter="{Binding Text, Source={x:Reference MySearchBar}}">
        <behaviours:TextChangedBehavior />

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.