Hi,
I am having some trouble figuring out how to bind my XAML controls in a GridTemplateColumn to the DataTable values when binding the sfDataGrid to a DataTable. I have a simple ContentPage that binds to a simple ViewModel. Here is the ViewModel:
using System.Data;
namespace ItemsList.ViewModels
{
class ItemsVM : VMBase
{
private DataTable _testTable = new DataTable();
public DataTable TestTable { get => _testTable; set => _testTable = value; }
public ItemsVM()
{
_testTable.Columns.Add("ItemNumber", typeof(int));
_testTable.Columns.Add("ItemName", typeof(string));
_testTable.Rows.Add(1, "Item 1");
_testTable.Rows.Add(2, "Item 2");
}
}
}
And here is the ContentPage:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:ItemsList.ViewModels"
xmlns:grid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
mc:Ignorable="d"
x:Class="ItemsList.Views.ItemsListPage">
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<StackLayout.BindingContext>
<vm:ItemsVM />
</StackLayout.BindingContext>
<grid:SfDataGrid x:Name="sfGrid"
ColumnSizer="Auto"
AllowEditing="False"
NavigationMode="Cell"
ItemsSource="{Binding TestTable}"
AutoGenerateColumns="True">
</grid:SfDataGrid>
</StackLayout>
</ContentPage.Content>
</ContentPage>
This works ok when the sfGrid auto-generates the columns. I'm seeing all the values. However, I want to use a GridTemplateColumn to display the ItemName in a Label, so I can use my own formatting. But I cannot figure out how to bind to the values of the ItemName column. I'm doing the following (the label binding is highlighted):
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:ItemsList.ViewModels"
xmlns:grid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
mc:Ignorable="d"
x:Class="ItemsList.Views.ItemsListPage">
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<StackLayout.BindingContext>
<vm:ItemsVM />
</StackLayout.BindingContext>
<grid:SfDataGrid x:Name="sfGrid"
ColumnSizer="Auto"
AllowEditing="False"
NavigationMode="Cell"
ItemsSource="{Binding TestTable}"
AutoGenerateColumns="True">
<grid:SfDataGrid.Columns>
<grid:GridTemplateColumn HeaderText="Name"
MappingName="ItemName">
<grid:GridTemplateColumn.CellTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Path=Item[1]}" />
</StackLayout>
</DataTemplate>
</grid:GridTemplateColumn.CellTemplate>
</grid:GridTemplateColumn>
</grid:SfDataGrid.Columns>
</grid:SfDataGrid>
</StackLayout>
</ContentPage.Content>
</ContentPage>
However, this is not working. I'm getting the following exception:
System.Reflection.AmbiguousMatchException
Message=Ambiguous match found.
How do I bind the properties of the Label in a GridTemplateColumn to the values in the DataTable?
Thanks a bunch for all your hard work and your assistance!
Alfredo