【问题标题】:Xamarin Forms SFDataGrid Binding IssueXamarin Forms SFDataGrid 绑定问题
【发布时间】: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


【解决方案1】:

您已将列的 LoadUIView 定义为 true,将其更改为 false,以便仅针对 Android 平台呈现文本。

供您参考。

在 Syncfusion DataGrid 中,仅在 Xamarin.Forms Android 中绘制文本以获得更好的性能,以便呈现文本。如果我们通过将所有列的 LoadUIView 设置为 true 来为同一场景加载 Label,那么您将在所有平台中面临渲染问题“单元格值不渲染”,这是因为 Xamarin 表单尚未提供对 Expando 对象的绑定支持.

在 Xamarin 表单中,ExpandoObject 绑定正在实施中。
https://github.com/xamarin/Xamarin.Forms/issues/3177

有关动态对象的 IOS 平台限制的更多详细信息,请参阅以下链接。
讨论链接:https://forums.xamarin.com/discussion/53941/expandoobject-crashing-ios
限制链接:https://docs.microsoft.com/en-gb/xamarin/ios/internals/limitations

建议您使用 ObservableCollection 或 DataTable 集合作为 SfDataGrid Itemsource.until Expando 对象绑定支持由 xamarin 表单框架提供。

问候,
普拉迪普库马尔 B

【讨论】:

  • 第一个示例使用 ExpandObject 并且工作正常。只有当我在循环中构建数据集时它才不起作用。
  • 嗨尼克,我有点困惑。由于 Xamarin 团队正在实施 ExpandObject 绑定支持。因此,请让我知道您那边哪个样本运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 2018-01-17
  • 1970-01-01
  • 2020-06-18
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多