【发布时间】:2017-09-09 18:00:45
【问题描述】:
我有一个绑定到自定义单元格的工作列表视图。但是,我想知道我是否真的必须拥有一个具有与自定义单元格属性一样命名的属性的模型。
我的自定义视单元(很多东西都遗漏了):
public class RecordListCell : ViewCell
{
public static readonly BindableProperty HeadingProperty = BindableProperty.Create ("Heading", typeof (string), typeof (RecordListCell), null);
public string Heading {
get { return (string)GetValue (HeadingProperty); }
set { SetValue (HeadingProperty, value); }
}
protected Label headingLbl { get; set; }
public RecordListCell ()
{
headingLbl = new Label () ;
headingLbl.SetBinding (Label.TextProperty, new Binding ("Heading"));
// from here on I construct a stacklayout and insert the label above
}
}
我的页面(很多东西遗漏了)
public TopicsPage ()
{
_topicList = new ListView ();
var cell = new DataTemplate (typeof (RecordListCell));
// NOT WORKING
// cell.SetBinding (RecordListCell.HeadingProperty, "Name");
// working (I must name the property exactly like the property in my custom cell)
cell.SetBinding (RecordListCell.HeadingProperty, "Heading");
_topicList.ItemTemplate = cell;
_topicList.ItemsSource = MyRepo.GetTopics();
}
所以上面的方法有效,但我不得不让 MyRepo.GetTopics() 返回具有名为 Heading 的属性的对象列表。我想将此自定义单元格与任何类型的对象列表一起重用,并像我的评论显示的那样在页面上指定绑定,但这没有用。
我在这里期待错误的事情还是我的方法错误?
【问题讨论】:
-
您是否希望同一页面的标题保持不变?还是您希望标题的内容根据项目的内容而动态变化?
-
同一页面的常量,但许多页面都希望使用相同的自定义单元格,因此我不希望该单元格需要命名模型属性
标签: xamarin data-binding xamarin.forms