2016-11-28 60 views
2

我想創建一個視圖,其中有多個控件位於StackLayout內部。當我只有一個這樣的:Xamarin窗體:在StackLayout中的ListView上佈局所需的幫助

<StackLayout Orientation="Vertical" HorizontalOptions="Center" > 
    <controls:BindablePicker /> 
</StackLayout> 

它工作正常: Centered

然而,當我嘗試添加一個ListView,如:

<StackLayout Orientation="Vertical" HorizontalOptions="Center" > 
    <controls:BindablePicker /> 
    <ListView HorizontalOptions="Center" > 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <ViewCell> 
      <ViewCell.View> 
      <StackLayout Orientation="Horizontal"> 
       <controls:BindablePicker Title="Length" /> 
       <controls:BindablePicker Title="Units" /> 
      </StackLayout> 
      </ViewCell.View> 
     </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    </ListView> 
</StackLayout> 

一切都是左對齊,並採取整個屏幕: Left justified

我想所有控件的寬度爲s儘可能集中在屏幕上。現在我正在使用ListView,因爲我綁定了一個對象列表,我認爲這會比嘗試使用Grid更容易。

任何建議將不勝感激。謝謝。

UPDATE我更新了我的ViewCell與網格替換StackLayout,然後把ListView的網格內,如下所示:

<Grid> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
    <ColumnDefinition Width="*" /> 
    <ColumnDefinition Width="2*" /> 
    <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <ListView Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" SeparatorVisibility="None"> 

這是一個很值得我想要的東西,不同的是頂部選取器正在增加屏幕的整個寬度,而不僅僅是它需要被居中的寬度。

回答

2

還有就是你的第一StackLayout例和第二例StackLayout(在ViewCell內的例子)之間的一個小的差異,這也是造成您遇到的麻煩:

  • 第一StackLayout例子是Orientation="Vertical"
  • 第二StackLayout示例(您的ViewCell內部的例子)是Orientation="Horizontal"

StackLayouts在Xamarin.Forms僅兌現HorizontalOptions p roperty當Orientation="Vertical",並且只有在Orientation="Horizontal"時纔會兌現VerticalOptions屬性。

StackLayouts的這個「特性」沒有很好的記錄,我找不到指定它的示例鏈接。就我個人而言,我在Xamarin大學班的一位講師身上學到了這個事實。

要使ViewCell的控件居中,請使用網格佈局而不是StackLayout。這裏有一個例子:

http://www.trsneed.com/customize-xamarin-forms-listview-with-viewcell/

+0

非常感謝您的回覆!我會試一試,讓我知道如果我不能工作。 –

+0

請參閱上面的更新。我更接近,但仍然缺少一塊。 –

+0

謝謝埃德!當你有機會時,讓我們將這個問題標記爲已回答,並將更新移到StackOverflow的新問題上。這將有助於任何未來的用戶提出同樣的問題,我非常樂意幫助您使用Grid! –