在ListView中,我想绑定一个网格columnSpan,然后在ItemSource对象中设置它。这是我试图实现这一点的方式,但我得到: ,对象引用没有设置为对象的实例“。我想知道什么是正确的方法,如果需要使用任何ValueConverter来设置span属性。 “标题”文本属性目前并不重要。
ViewCell:
class CustomPerformanceCell : ViewCell
{
public CustomPerformanceCell()
{
var titleLabel = new Label();
titleLabel.SetBinding(Label.TextProperty , "Title");
var resultGrid = new Grid
{
HorizontalOptions = LayoutOptions.FillAndExpand
};
resultGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
resultGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
resultGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
resultGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
resultGrid.BackgroundColor = Color.DarkGray;
var ColorBox = new BoxView();
ColorBox.SetBinding(VisualElement.BackgroundColorProperty, "BackgroundColor", BindingMode.Default, new BackgroundConverter(), null);
resultGrid.Children.Add(ColorBox ,0,0);
ColorBox.SetBinding(Grid.ColumnSpanProperty, "Performance");
}
}
PerformanceCell:
public class PerformanceCellItem
{
public string Title { get; set; }
public int Performance { get; set; }
public string BackgroundColor { get; set; }
public PerformanceCellItem(string title, int performance ,string backgroundColor)
{
Title = title;
Performance = performance;
BackgroundColor = backgroundColor;
}
}
创建它的功能:
public void PreparePerformanceStack()
{
PerformanceList = new ListView
{
ItemTemplate = new DataTemplate(typeof(CustomPerformanceCell))
};
ObservableCollection<PerformanceCellItem> cellItems = new ObservableCollection<PerformanceCellItem>
{
new PerformanceCellItem("test 1", 2,"#00AA00"),
new PerformanceCellItem("test 2", 2,"#00AA00"),
new PerformanceCellItem("test 3", 2,"#00AA00")
};
PerformanceList.ItemsSource = cellItems;
}
答案 0 :(得分:1)
ColumnSpan
除非有父网格,否则无法设置为Grid
;如果您想将其设置为BoxView
- 请使用colorBox.SetBinding(Grid.ColumnSpanProperty, "Performance")
。
另外,请确保Performance
的值为&gt; = 1(否则coerce condition将引发异常)。只要源类型为int或parse-able string,您就不需要值转换器。
编辑1 - 根据问题编辑
您需要在自定义视图单元格中指定View
属性
this.View = resultGrid;
在您的构造函数CustomPerformanceCell()