【发布时间】:2020-06-25 14:58:11
【问题描述】:
我在绑定到数据网格时遇到问题。我有一个动态数据集,所以我不能只创建对象。我试过同时使用 ExpandoObjects 数组和 DataTable,结果是一样的。如果我明确创建数据集,它可以工作,但这对我没有任何好处。我的xml是:
<sf:SfDataGrid BackgroundColor="{ DynamicResource DataGridHeaderRowBackgroundColor }" ItemsSource="{Binding TableData, Mode=OneWay}" Columns="{Binding Columns, Mode=OneWay}" AutoGenerateColumns="False"></sf:SfDataGrid>
例如:
int cellIndex = 0;
foreach (TableCell cell in row.Cells.OrderBy(c => c.ColumnIndex))
{
GridTextColumn column = new GridTextColumn
{
HeaderText = cell.CellText
};
if (cell.CellType == TableCellType.Checkbox)
{
column.ColumnSizer = ColumnSizer.SizeToHeader;
}
else
{
column.Width = 200;
}
column.MappingName = cellIndex.ToString();
column.HeaderFontAttribute = FontAttributes.Bold;
column.HeaderTextAlignment = TextAlignment.Center;
column.HeaderFont = "SourceSansPro-Bold.ttf#Source Sans Pro";
column.HeaderCellTextSize = 14;
column.LoadUIView = true;
column.LineBreakMode = LineBreakMode.CharacterWrap;
this.Columns.Add(column);
cellIndex++;
}
Dictionary s = new Dictionary();
s.Add("0", "zero");
s.Add("1", "one");
s.Add("2", "two");
s.Add("4", "three");
s.Add("5", "four");
s.Add("6", "five");
s.Add("7", "six");
s.Add("8", "seven");
s.Add("8", "eight");
tableData.Add(s.ToExpando());
this.TableData = tableData;
现在,对于我的真实数据,我这样做(列的内容完全相同)。这不起作用,我得到了附加的屏幕截图。
Dictionary rowData = new Dictionary();
int cellIndex = 0;
foreach (TableCell cell in row.Cells.OrderBy(c => c.ColumnIndex))
{
rowData.Add(cellIndex.ToString(), string.IsNullOrEmpty(cell.CellText) ? "EMPTY" : cell.CellText);
cellIndex++;
}
tableData.Add(rowData.ToExpando());
【问题讨论】:
-
有趣的是,如果我使用 Visual Studio 并更改 xaml(我正在测试背景颜色),网格会正确呈现。
标签: xamarin.forms datagrid syncfusion