2017-02-20 5 views
0

마치 높이가 Items이고 높이가 ItemsListView과 같아야합니다.ListView의 높이를 Xamarin.Form에있는 ListViewItems의 높이와 같게 설정하는 방법은 무엇입니까?

높이를 얻는 방법 ListViewItem?

myListView.RowHeight-1입니다.

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을 사용하지 않음) :

나는 ListView이 방법을 선언

내 페이지 ListView ISN의 0 ListViewItems (크기 보이는 방법
<NavigationPage> 
    <ContentPage> 
     <ScrollView> 
      <StackLayout> 
       <ListView> 
       ... 

값의 수에 따라 변함) :

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; 
관련 문제