本演练说您可以在一行中创建WPF数据网格,但不提供完整的示例.
所以我创建了一个使用通用列表并将其连接到WPF数据网格的示例,但它没有显示任何数据.
我需要更改下面的代码才能让它在datagrid中显示数据?
此代码现在有效:
XAML:
<Window x:Class="TestDatagrid345.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:TestDatagrid345"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<StackPanel>
<toolkit:DataGrid ItemsSource="{Binding}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System.Collections.Generic;
using System.Windows;
namespace TestDatagrid345
{
public partial class Window1 : Window
{
private List<Customer> _customers = new List<Customer>();
public List<Customer> Customers { get { return _customers; }}
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataContext = Customers;
Customers.Add(new Customer { FirstName = "Tom", LastName = "Jones" });
Customers.Add(new Customer …Run Code Online (Sandbox Code Playgroud)