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

  • Question

  • User399270 posted

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

    https://stackoverflow.com/questions/46588861/xamarin-forms-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

    ```

    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)
                    {
    
                    }
                }
            };
    

    }

    ```

    Thursday, February 11, 2021 1:39 PM

Answers

  • User369978 posted

    Just simply set searchResults.HeightRequest in SearchBar_TextChanged method .

    ``` void SearchBar_TextChanged(object sender, EventArgs e) { int numberOfProducts = 0; SearchListView.ItemsSource = GetProducts(out numberOfProducts, e1.NewTextValue);

            SearchListView.RowHeight = 50;
            SearchListView.HeightRequest = numberOfProducts * 50;
    
        }
    

    ```

    And set ListView's VerticalOptions as Start in xaml

    Test (The green area represent ListView )


    Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, February 12, 2021 5:17 AM

All replies

  • User369978 posted

    Just simply set searchResults.HeightRequest in SearchBar_TextChanged method .

    ``` void SearchBar_TextChanged(object sender, EventArgs e) { int numberOfProducts = 0; SearchListView.ItemsSource = GetProducts(out numberOfProducts, e1.NewTextValue);

            SearchListView.RowHeight = 50;
            SearchListView.HeightRequest = numberOfProducts * 50;
    
        }
    

    ```

    And set ListView's VerticalOptions as Start in xaml

    Test (The green area represent ListView )


    Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, February 12, 2021 5:17 AM
  • User399270 posted

    Ahh thats great thanks for the help ColeX

    Friday, February 12, 2021 12:33 PM