Reputation: 413
So as I was following the documentation about DataGrid
using the windows toolkit, as I progress down. There is a sample code
This one
<controls:DataGrid x:Name="dataGrid1"
Height="600" Margin="12"
AutoGenerateColumns="True"
ItemsSource="{x:Bind MyViewModel.Customers}" />
And this is the code on my side
<controls:DataGrid x:Name="dgvTest"
Height="800"
Margin="1"
AutoGenerateColumns="True"
ItemsSource="{x:Bind }">
As I was trying it out. I can't seem to find where did MyViewModel came from.
Going further they have this code
//backing data source in MyViewModel
public class Customer
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String Address { get; set; }
public Boolean IsNew { get; set; }
public Customer(String firstName, String lastName,
String address, Boolean isNew)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Address = address;
this.IsNew = isNew;
}
public static List<Customer> Customers()
{
return new List<Customer>(new Customer[4] {
new Customer("A.", "Zero",
"12 North Third Street, Apartment 45",
false),
new Customer("B.", "One",
"34 West Fifth Street, Apartment 67",
false),
new Customer("C.", "Two",
"56 East Seventh Street, Apartment 89",
true),
new Customer("D.", "Three",
"78 South Ninth Street, Apartment 10",
true)
});
}
}
So definitely MyViewModel is not a class because Customer
is the class, and the sammple line on the GitHub page has this line
private DataGridDataSource viewModel = new DataGridDataSource();
But whenever I try to add that to my code, I encounter an error which is this
Error CS0246 The type or namespace name 'DataGridDataSource' could not be found (are you missing a using directive or an assembly reference?)
So sorry if I sound like an amateur, but when I was using DataGridView
using WinForms, I never had encountered this issues.
Any help would be appreciated. Thanks
Upvotes: 0
Views: 282
Reputation: 169360
The DataGridDataSource
class is found here: https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/35ffc09c4cba6354eb7d9dcac1f97c554ac5df68/Microsoft.Toolkit.Uwp.SampleApp/Data/DataGridDataSource.cs
If you x:Bind
to MyViewModel.Customers
in your XAML, MyViewModel
should be a property of the page class that returns an instance of a class that has a Customers
property that returns a List<Customer>
:
public class DataGridDataSource
{
public List<Customer> Customers => Customer.Customers();
}
public sealed partial class MainPage : Page
{
public DataGridDataSource MyViewModel => new DataGridDataSource();
public MainPage()
{
InitializeComponent();
}
}
If you look at the last example in the docs, you see that the MainPage.xaml.cs
class has a List<Person>
property called "Persons" that the DataGrid
binds to:
ItemsSource="{x:Bind Persons}"
Upvotes: 1