2017-02-20 5 views
0

私はちょうどの数とItemsの数に等しいListViewの高さを設定する必要があるように見えます。ListViewの高さを、すべてのListViewItemsの高さと同じにする方法は、Xamarin.Formでですか?

しかし、高さを取得する方法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

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; 
関連する問題