Resize ListView (inside stacklayout) after each keyboard entry from code behind

79 Views Asked by At

-> xamarin forms resize listview (inside stacklayout) after each keyboard entry - from code behind

(would like this cross platform if it is possible?)

Using SearchBar to allow user to search for products of wines... when text is changed the displayed results are updated correctly but I want to change the size of the ListView to only show results removing any empty white space.

So for example in attached picture entry 'Wine ' returns 12 results, which shows all....but 'Wine 1' only 4 results. So I would like the ListView to end after 'wine 12'(from the results) instead the opacity covers the rest of the screen.

Have tried some examples such as: ListView inside StackLayout: How to auto resize the ListView?

but still cant get it going does anyone see what I am doing wrong thank you

    <NavigationPage.TitleView>
            <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                <SearchBar x:Name="SearchBar" TextChanged="SearchBar_TextChanged" HorizontalOptions="FillAndExpand" Placeholder="Search..." PlaceholderColor="Gray" TextColor="White" VerticalOptions="StartAndExpand"/>
            </StackLayout>
        </NavigationPage.TitleView>
     private void SearchBar_TextChanged(object sender1, TextChangedEventArgs e1)
            {
                StackSearchResults.IsVisible = true;
                int numberOfProducts = 0;
                SearchListView.ItemsSource = GetProducts(out numberOfProducts, e1.NewTextValue);
    
                SearchListView.RowHeight = 50;
    
                SearchListView.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) =>
                {
                    if (e.PropertyName == "ItemsSource")
                    {
                        try
                        {
                            if (SearchListView.ItemsSource != null)
                            {
                                SearchListView.HeightRequest = numberOfProducts * 50;
                            }
                        }
                        catch (Exception ex)
                        {
    
                        }
                    }
                };
    }

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

you only need to assign the PropertyChanged handler once, not every time TextChanged fires. That is probably not the root issue, but it's not helping. You should also be sure you're assigning the HeightRequest on the UI thread