http://opensource.org/licenses/MIT
MainPage.XAML:-
--------------------------
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SearchbarExample"
x:Class="SearchbarExample.MainPage">
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<SearchBar x:Name="sbMedicionName" Placeholder="What tablets you are using?" TextChanged="searchTabletName" VerticalOptions="Start" />
<StackLayout x:Name="slNewItem">
<Label Text="No search found" FontSize="20"/>
</StackLayout>
<ListView x:Name="lbxMedicionsList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label x:Name="lablName" Text="{Binding MedicionName}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
MainPage.XAML.Cs:-
--------------------------
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Collections.ObjectModel;
namespace SearchbarExample
{
public partial class MainPage : ContentPage
{
ObservableCollection<Medision> objMedicionsList = null;
public MainPage()
{
InitializeComponent();
bindMedicionData();
slNewItem.IsVisible = false;
}
public void bindMedicionData()
{
objMedicionsList = new ObservableCollection<Medision>();
objMedicionsList.Add(new Medision { MedicionName = "venkatrao" });
objMedicionsList.Add(new Medision { MedicionName = "chiranjeevi" });
objMedicionsList.Add(new Medision { MedicionName = "sivakonda" });
objMedicionsList.Add(new Medision { MedicionName = "sudhakar" });
objMedicionsList.Add(new Medision { MedicionName = "venky" });
objMedicionsList.Add(new Medision { MedicionName = "chinappureddy" });
//lbxMedicionsList.ItemsSource = objMedicionsList;
}
public void searchTabletName(Object sender,TextChangedEventArgs e)
{
string strMedicionName = e.NewTextValue.ToLower().ToString();
//var medicionsList = (from s in objMedicionsList where s.MedicionName.Contains(strMedicionName) select s).ToList();
// objMedicionsList.Clear();
if (strMedicionName.Length > 0) {
var medicionsList = (from s in objMedicionsList where s.MedicionName.StartsWith(strMedicionName) select s).ToList();
if (medicionsList.Count > 0)
{
lbxMedicionsList.ItemsSource = medicionsList;
slNewItem.IsVisible = false;
lbxMedicionsList.IsVisible = true;
}
else
{
slNewItem.IsVisible = true;
lbxMedicionsList.IsVisible = false;
}
}
}
}
}
MainPage.XAML:-
--------------------------
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SearchbarExample"
x:Class="SearchbarExample.MainPage">
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<SearchBar x:Name="sbMedicionName" Placeholder="What tablets you are using?" TextChanged="searchTabletName" VerticalOptions="Start" />
<StackLayout x:Name="slNewItem">
<Label Text="No search found" FontSize="20"/>
</StackLayout>
<ListView x:Name="lbxMedicionsList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label x:Name="lablName" Text="{Binding MedicionName}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
MainPage.XAML.Cs:-
--------------------------
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Collections.ObjectModel;
namespace SearchbarExample
{
public partial class MainPage : ContentPage
{
ObservableCollection<Medision> objMedicionsList = null;
public MainPage()
{
InitializeComponent();
bindMedicionData();
slNewItem.IsVisible = false;
}
public void bindMedicionData()
{
objMedicionsList = new ObservableCollection<Medision>();
objMedicionsList.Add(new Medision { MedicionName = "venkatrao" });
objMedicionsList.Add(new Medision { MedicionName = "chiranjeevi" });
objMedicionsList.Add(new Medision { MedicionName = "sivakonda" });
objMedicionsList.Add(new Medision { MedicionName = "sudhakar" });
objMedicionsList.Add(new Medision { MedicionName = "venky" });
objMedicionsList.Add(new Medision { MedicionName = "chinappureddy" });
//lbxMedicionsList.ItemsSource = objMedicionsList;
}
public void searchTabletName(Object sender,TextChangedEventArgs e)
{
string strMedicionName = e.NewTextValue.ToLower().ToString();
//var medicionsList = (from s in objMedicionsList where s.MedicionName.Contains(strMedicionName) select s).ToList();
// objMedicionsList.Clear();
if (strMedicionName.Length > 0) {
var medicionsList = (from s in objMedicionsList where s.MedicionName.StartsWith(strMedicionName) select s).ToList();
if (medicionsList.Count > 0)
{
lbxMedicionsList.ItemsSource = medicionsList;
slNewItem.IsVisible = false;
lbxMedicionsList.IsVisible = true;
}
else
{
slNewItem.IsVisible = true;
lbxMedicionsList.IsVisible = false;
}
}
}
}
}
Medision.Cs:-
----------------------
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SearchbarExample
{
public class Medision
{
public string MedicionName { get; set; }
}
}
No comments:
Post a Comment