Issue
I have SearchBar which in real-time search information from List.But I have a problem.for example when I write the text in the field the new text is displayed but when I erase a part of the text it is not updated to the previous state. That is if I have list:
BAC
BAE
BAIC
Bailey
Bajaj
Baker
Electric
Baldwin-Motion
Ballot
When I write for example Bai or bai my list show me
BAIC
Bailey But when erase the letter(now ba) it must show BAC
BAE
BAIC
Bailey
Bajaj
Baker
But it is still BAIC
Bailey
Please help
Xaml Code
<Frame CornerRadius="15" HasShadow="True" BorderColor="LightGray" Margin="0,10,0,0" Padding="5">
<controls:CustomSearchBar x:Name="searchBar"
TextColor="Gray"
>
<!----> <corcav:Interaction.Behaviors>
<corcav:BehaviorCollection>
<corcav:EventToCommand EventName="TextChanged" Command="{Binding Path=SearchBarTextChangedCommand}" PassEventArgument="True"/>
</corcav:BehaviorCollection>
</corcav:Interaction.Behaviors>
</controls:CustomSearchBar>
</Frame>
<ListView x:Name="Company" SelectionMode="None" ItemsSource="{Binding SearchedCars}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="46" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackLayout>
<controls:ExtendedButton2 HeightRequest="45" Padding="5,10,0,10"
WidthRequest="300" Margin="0" HorizontalTextAlignment="Start"
BackgroundColor="{Binding CustButtonColor}" Text="{Binding Name}" TextColor="Gray" FontSize="18"
Command="{ Binding BindingContext.ChangeMark,
Source={x:Reference Name=Company} }" CommandParameter="{Binding .}"/>
<BoxView Margin="0,-6,0,0"
HeightRequest="1"
BackgroundColor="LightGray"
HorizontalOptions="FillAndExpand" VerticalOptions="End"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
ViewModel
class CarAddCompany : INotifyPropertyChanged
{
private List<Car> Cars = new List<Car>();
public List<Car> SearchedCars { get => Cars; set { Cars = value; OnPropertyChanged("SearchedCars"); } }
public ICommand ChangeMark { protected set; get; }
public MainCar mainCar { get; set; }
INavigation Navigation { get; set; }
public Command navigateCommand { get; set; }
public Command navigateCommandBACK { get; set; }
public ICommand SearchBarTextChangedCommand { get; set; }
public async Task GotoPage2()
{
if (mainCar.MarkCar != null) await Navigation.PushModalAsync(new Car_add_model(mainCar));
}
public async Task GotoPage1()
{
//mainCar.MarkCar ="";
// await Navigation.PushModalAsync(new Car_add());
await Navigation.PopModalAsync();
}
private void OnSearchBarTextChanged(object obj)
{
if (obj is TextChangedEventArgs args)
{
string filter = args.NewTextValue;
SearchedCars = Cars.Where(x => x.Name.ToLower().Contains(filter.Trim().ToLower())).ToList();
}
}
public CarAddCompany(INavigation navigation,MainCar carC)
{
mainCar = carC;
this.Navigation = navigation;
this.navigateCommand = new Command(async () => await GotoPage2());
navigateCommandBACK = new Command(async () => await GotoPage1());
SearchBarTextChangedCommand = new Command<object>(OnSearchBarTextChanged);
Cars = new List<Car>();
Cars.Add(new Car() { Name = "BAC", CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "BAE", CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "BAIC", CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "Bailey", CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "Bajaj", CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "Baker", CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "Electric", CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "Baldwin-Motion",CustButtonColor = Color.White });
Cars.Add(new Car() { Name = "Ballot", CustButtonColor = Color.White });
foreach (var item in Cars)
{
item.CustButtonColor = Color.White;
}
var car = key as Car;
car.CustButtonColor = Color.FromHex("#D1DAED");
mainCar.MarkCar = car.Name;
});
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
I want that when I erase a word I show the previous list with Ba. Depending the text in searchbar
Solution
Update this and Debug this method
private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
string filter = e.NewTextValue;
if (!string.IsNullOrEmpty(filter))
{
var filterList = cars.Where(x => x.Name.ToLower().Contains(filter.Trim().ToLower())).ToList();
SearchedCars = new ObservableCollection<Car>(filterList);
}
else
SearchedCars = new ObservableCollection<Car>(cars);
}
Answered By - Shubham Tyagi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.