2017-02-20 89 views
0

我已經爲我的ListView創建DataTemplate,但不顯示ListViewItems的文本。如何綁定ItemsSource的文本?添加到您的StackLayout前在沒有XAML的情況下在ListView DataTemplate中將ItemsSource綁定到標籤

List<string> stringList; 

ListView myListView = new ListView 
{ 
    ItemsSource = stringList, 
    ItemTemplate = new DataTemplate(() => 
    { 
     StackLayout sl = new StackLayout 
     { 
      VerticalOptions = LayoutOptions.FillAndExpand, 
      Orientation = StackOrientation.Horizontal, 
     }; 
     sl.Children.Add(new Label 
     { 
      FontSize = 14, 
     }); 

     return new ViewCell { View = sl }; 
}) 

回答

1

初始化您的標籤,稱之爲temp例如和bind像這樣一個自定義對象:

temp.SetBinding (Label.TextProperty, "TemplatePropertyThatYouWish"); 

而這款以字符串列表:

temp.SetBinding(Label.TextProperty, "."); 

然後將其添加到您的StackLayout。

代碼:

List<string> stringList = new List<string> { "a", "b", "c" }; 

ListView myListView = new ListView 
{ 
    ItemsSource = stringList, 
    ItemTemplate = new DataTemplate(() => 
    { 
     StackLayout sl = new StackLayout 
     { 
      VerticalOptions = LayoutOptions.FillAndExpand, 
      Orientation = StackOrientation.Horizontal, 
     }; 

     var temp = new Label 
     { 
      FontSize = 14, 
     }; 

     temp.SetBinding(Label.TextProperty, "."); 

     sl.Children.Add(temp); 

     return new ViewCell { View = sl }; 
    }) 
}; 
+0

但我如果我需要我的ItemSource綁定字符串列表使用什麼第二個參數? – InfernumDeus

+0

已更新@InfernumDeus – Zroq

相關問題