简体   繁体   English

根据Xamarin表单中的项目数设置Listview高度

[英]Set Listview height based on the number of items in Xamarin Forms

I have a ListView that shows suggestions. 我有一个显示建议的ListView How can I set the Height of my list view based on the number of items? 如何根据项目数设置列表视图的Height

The maximum number of items is 10 and the minimum is 1. 物品的最大数量为10,最小为1。

I have added a height request code base on the number of results still not working 我已根据结果数量添加了身高要求代码,但仍无法正常工作

My XAML: 我的XAML:

<Label Text="RETAILER NAME" StyleClass="lbl-fieldform"/>
                <Entry x:Name="NameSearch" StyleClass="fieldForm" Focused="NameSearch_Focused" Unfocused="NameSearch_Unfocused" TextChanged="NameSearch_TextChanged"/>
                <ListView x:Name="lstName" IsVisible="False" HeightRequest="50" ItemTapped="lstName_ItemTapped">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout StyleClass="lstContainer">
                                    <Label StyleClass="lstName" Text="{Binding FileAs}"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
                <Label Text="RETAILER CODE" StyleClass="lbl-fieldform"/>
                <Picker Title="Select Retailer Code" x:Name="codePicker" ItemsSource="{Binding RetailerCode}" ItemDisplayBinding="{Binding RetailerCode}" StyleClass="fieldForm" IsEnabled="False"/>
                <Label Text="STREET" StyleClass="lbl-fieldform"/>
                <Entry IsEnabled="false" StyleClass="fieldForm" x:Name="entStreet"/>

My formula for the height: 我的身高公式:

var db = DependencyService.Get<ISQLiteDB>();
var conn = db.GetConnection();

string sql = "SELECT * FROM tblContacts WHERE FileAs LIKE '%" + keyword + "%' ORDER BY FileAs LIMIT 10";
var getUser = conn.QueryAsync<ContactsTable>(sql);
var resultCount = getUser.Result.Count;

if (resultCount > 0)
{
    var result = getUser.Result;
    lstName.HeightRequest = (resultCount * 50) + (10 * resultCount);
    lstName.ItemsSource = result;
}
else
{
    lstName.IsVisible = false;
}

Remove + (10 * resultCount); 删除+(10 * resultCount); in your code 在你的代码中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM