UWP DataGrid文档不清晰,使用Windows Toolkit遇技术问题求助
Hey there, let's break down how to fix this issue step by step—it's a common gotcha when moving from WinForms DataGridView to Windows Toolkit DataGrid!
First: Understand what MyViewModel (or DataGridDataSource) actually is
The docs use MyViewModel as a stand-in for a custom view model class that holds your data. It's not a built-in part of the Toolkit—you need to create it yourself (or replicate the DataGridDataSource from the GitHub sample, which is just a custom view model).
WinForms let you bind directly to static methods or raw lists easily, but WinUI/UWP's x:Bind requires a clear, accessible data source instance (the view model) to work properly.
Step 1: Create your view model class
Let's build the DataGridDataSource class from the GitHub sample (you can also name it MyViewModel if you prefer). This class will hold your Customers data as a bindable property:
public class DataGridDataSource { // Expose the Customers list as a property for binding public List<Customer> Customers { get; } public DataGridDataSource() { // Initialize the data using your existing Customer static method Customers = Customer.Customers(); } } // Keep your existing Customer class (tweak the static method name if you want clarity) public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public bool IsNew { get; set; } public Customer(string firstName, string lastName, string address, bool isNew) { FirstName = firstName; LastName = lastName; Address = address; IsNew = isNew; } public static List<Customer> GetCustomers() { return new List<Customer> { 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) }; } }
Step 2: Add the view model to your page/window
In your XAML code-behind (e.g., MainPage.xaml.cs or MainWindow.xaml.cs), declare an instance of your view model. Make sure it's accessible to x:Bind (use public or internal):
public sealed partial class MainPage : Page { // Create the view model instance public DataGridDataSource ViewModel { get; } = new DataGridDataSource(); public MainPage() { this.InitializeComponent(); } }
Step 3: Fix your XAML binding
Update your DataGrid's ItemsSource to bind to the view model's Customers property:
<controls:DataGrid x:Name="dgvTest" Height="800" Margin="1" AutoGenerateColumns="True" ItemsSource="{x:Bind ViewModel.Customers}" />
Step 4: Fix the "type not found" error
The CS0246 error happens because DataGridDataSource isn't a built-in class—it's a custom class from the GitHub sample. Once you create the class above, make sure:
- It's in the same namespace as your page/window, or
- You add a namespace reference in your XAML if it's in a different folder:
xmlns:local="using:YourAppName.YourViewModelFolder"
Quick note on WinForms vs WinUI DataGrid
WinForms DataGridView is more flexible with direct bindings to static methods or loose lists, but WinUI's x:Bind is type-safe and relies on explicit data contexts (view models) to keep your code organized. This pattern is standard for modern Windows apps!
内容的提问来源于stack exchange,提问作者Nate

