2017-02-20 47 views
0

看起來我只需要設置高度ListView等於Items乘以Items的高度。如何在Xamarin.Form中將ListView的高度設置爲等於所有ListViewItems的高度?

但是如何獲得高度爲ListViewItem

myListView.RowHeight只是顯示-1

我宣佈ListView這樣:

DataTemplate dTemplate = new DataTemplate(() => { 
    StackLayout sl = new StackLayout { 
     VerticalOptions = LayoutOptions.FillAndExpand, 
     Orientation = StackOrientation.Horizontal, 
     Padding = new Thickness(20, 10, 0, 10), 
     Spacing = 20 
    }; 

    var temp = new Label { FontSize = 20 }; 
    temp.SetBinding(Label.TextProperty, "."); 

    sl.Children.Add(temp); 

    return new ViewCell { View = sl }; 
}); 

ListView myListView = new ListView { 
    VerticalOptions = LayoutOptions.FillAndExpand, 
    ItemsSource = stringList, 
    ItemTemplate = dTemplate 
    BackgroundColor = Color.Red 
}; 

我用以下結構(我不使用XAML):

<NavigationPage> 
    <ContentPage> 
     <ScrollView> 
      <StackLayout> 
       <ListView> 
       ... 

這是我的頁面的外觀與0 ListViewItemsListView ISN的大小不隨數值變化):

enter image description here

+0

StackLayout就夠了;如果您的項目總高度高於屏幕高度,則列表視圖將會過大。 – Bonelol

回答

1

您的ListView VerticalOptions不應該設置爲LayoutOptions.FillAndExpand,以便擴展以適合所有的孩子?

更新

LayoutOptions應設置爲僅Fill,沒有必要擴大。它將適合你的屏幕高度或佈局,如果你定義一個。

代碼:

List<string> stringList = new List<string> { "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", }; 
     DataTemplate dTemplate = new DataTemplate(() => 
     { 
      StackLayout sl = new StackLayout 
      { 
       Orientation = StackOrientation.Horizontal, 
       Padding = new Thickness(20, 10, 0, 10), 
       Spacing = 20 
      }; 

      var temp = new Label { FontSize = 20 }; 
      temp.SetBinding(Label.TextProperty, "."); 

      sl.Children.Add(temp); 

      return new ViewCell { View = sl }; 
     }); 

     ListView myListView = new ListView 
     { 
      VerticalOptions = LayoutOptions.Fill, 
      ItemsSource = stringList, 
      ItemTemplate = dTemplate, 
      BackgroundColor = Color.Red 
     }; 

     this.Content = myListView; 
+0

它不起作用。 – InfernumDeus

+0

顯示您的代碼,以便我們可以進一步幫助您。 – Zroq

+0

哪個元素是你的'myListView'的父元素? – Zroq

0

的代碼此字符串的工作與我的項目還挺不錯,但我很懷疑這是一個很好的做法。

myListView.RequestHeight = number_of_items * 45; 
相關問題