2017-08-01 29 views
0

我試圖在ListView組件中顯示兩個TextCell。五月MainPage.xaml代碼:無法顯示兩個TextCell

<StackLayout> 

    <Button Text="Go To Map Page" Clicked="Button_OnClicked"/> 

    <ListView x:Name="MyList"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <TextCell Text="{Binding Name}" /> 
       <TextCell Text="{Binding Age}" /> 
      </DataTemplate> 

     </ListView.ItemTemplate> 

    </ListView> 

</StackLayout> 

CS代碼:

namespace App20 
{ 
    public partial class MainPage : ContentPage 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
      MyList.ItemsSource = new List<Person> 
      { 
       new Person() 
       { 
        Name = "Dima", 
        Age = 20 
       }, 
       new Person() 
       { 
        Name = "Roma", 
        Age = 21 
       }, 
       new Person() 
       { 
        Name = "Masha", 
        Age = 19 
       } 


      }; 
     } 

     private async void Button_OnClicked(object sender, EventArgs e) 
     { 
      await Navigation.PushAsync(new MapPage()); 
     } 
    } 
} 

如果我只顯示元素之一(姓名,年齡),一切工作正常。如果我試圖顯示他們兩個我的應用程序掛起。

第二個問題。爲什麼只有TextCell有效?我試圖用ButtonLabel,並得到例外。

下面是截圖:

enter image description here

enter image description here

+1

創建自己的自定義單元格的佈局,我認爲這是一個重複:https://stackoverflow.com/questions/25052746/xamarin-forms-binding-multiple-textcells-in-one-listview – cotnic

+0

你必須爲你的數據模板使用一個容器視圖,例如'StackLayout'或'Grid' – Milen

回答

1

你不能在一個ListView控件使用兩個單元。您可以使用TextCell的詳細屬性來顯示文本

<DataTemplate> 
    <TextCell Text="{Binding Name}" Detail="{Binding Age}" /> 
    </DataTemplate> 

的2號線,也可以使用ViewCell