2017-08-30 59 views
1

我有一个ListView及其ItemsSource。Xamarin表单ListView ItemsSource问题

ListView IList = new ListView(); 
IList.ItemsSource = _routine_names; 

为了自定义数据模板的每个项目我做:

IList.ItemTemplate = new DataTemplate(()=> 
     { 
      Label Routine_Name = new Label(); // should be_routine_names 
      return new ViewCell 
      { 
       View = new StackLayout{ 
        Children = {Routine_Name} 
       } 
      }; 
     }); 

当我运行,这是我的显示ListView和列表项确实存在(它们的onclick处理程序工作),但没有文字,应该是_routine_names中的任何文字。

我的问题如何让DataTemplate中的标签成为_routine_names中的项目?

我添加DataTemplate的原因是我可以添加滑动删除。

回答

4

您可以使用内置的TextCell来实现您想要做的事情。它有一个可绑定的TextProperty和一个可选的DetailProperty,如果你想要一个较小的文本行下面的主要。

IList.ItempTemplate = new DataTemplate(()=> 
{ 
    var textCell = new TextCell(); 
    textCell.ContextActions.Add(yourAction); 
    textCell.SetBinding(TextCell.TextProperty, "."); 
    return textCell; 
} 

IList.ItemsSource = _routine_names; 

yourAction是类型的菜单项所看到here

而且,请注意,IList是System.Collection命名空间的类的名字,所以我会用别的东西存在。

+0

什么是第二个参数中的“Routine_Name”?我的_routine_names只是一个字符串的ObservableCollection。 – justin

+0

@justin如果它只是ObservableCollection ,您可以将绑定设置为'.',它将指向集合中的每个字符串。我更新了我的答案以反映变化。 – hankide

+0

@justing你有没有得到它与最新的变化? – hankide