2017-05-17 26 views
0

我有一個ListView,下面是一個StackLayout。我試圖使ListView的高度隨着更多行的增加而增加(使用StackLayout中的命令),只使用XAML。發生了什麼事是ListView控件是高度過大和頁面結束了滾動與StackLayout一路下跌如何讓ListView高度動態?

這裏是我的代碼:

<StackLayout Orientation="Vertical"> 
    <BoxView HorizontalOptions="FillAndExpand" HeightRequest="10" Color="Transparent/> 
    <StackLayout> 
      <Label Style="{StaticResource AXASOL_Label_H1}" TextColor="White" Text="Do you have Dependents?" HorizontalOptions="Center"/> 
    </StackLayout> 
    <ListView> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
    </ListView> 
    <StackLayout Orientation="Horizontal" HorizontalOptions="Center"> 
    </StackLayout> 
</StackLayout> 

回答

2

我會建議使用Grid代替StackLayout。確保包含ListView的已設置爲Height="*"

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Label Grid.Row="0" Content="Do you have Dependents?" /> 

    <ListView Grid.Row="1" ItemsSource="{Binding Items}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}" /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
    <Button Grid.Row="2" Content="Click" Click="ButtonBase_OnClick"></Button> 
</Grid> 

代碼隱藏:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
    InitializeComponent(); 
    DataContext = this; 
    Items = new ObservableCollection<Item>(); 
    Items.Add(new Item("bla")); 
    Items.Add(new Item("blub")); 
    Items.Add(new Item("blubber")); 
} 

    public ObservableCollection<Item> Items { get; set; } 

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e) 
    { 
    Items.Add(new Item("test")); 
    } 
} 

public class Item 
{ 
    public Item(string name) 
    { 
    Name = name; 
    } 
    public string Name { get; set; } 
} 

這將使所有RowsHeight="Auto"採取儘可能多的空間及其內容的需求和含ListView將填補所有的剩餘空間。

+0

我試過這個,但是隨着列表增長,最後一行不下去 –

+0

@AlfieMcNarutoad我編輯了我的文章。按鈕點擊現在將添加一個新項目到列表視圖。我不明白你最後一排的意思是不會下降。你想添加一個新項目發生什麼? – user3292642

+0

@AlfieMcNarutoad你可以嘗試設置包含ListView的行的高度爲「Auto」嗎?這是否解決了您的問題? – user3292642