本文介绍了为什么我的WPF Datagrid不显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此表示您可以创建一个WPF datagrid在一行,但没有给出一个完整的例子。
This walkthrough says you can create a WPF datagrid in one line but doesn't give a full example.
所以我创建了一个使用通用列表并将其连接到WPF数据网格的示例,但它没有不要显示任何数据。
So I created an example using a generic list and connected it to the WPF datagrid, but it doesn't show any data.
需要更改以下代码才能在datagrid中显示数据?
What do I need to change on the code below to get it to show data in the datagrid?
此代码现在可用:
XAML: strong>
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>
代码背后:
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 { FirstName = "Joe", LastName = "Thompson" });
Customers.Add(new Customer { FirstName = "Jill", LastName = "Smith" });
}
}
}
推荐答案
现在从绑定中删除Path = Customers,它应该可以正常工作:)
Now remove the Path=Customers from your binding, and it should work :)
这篇关于为什么我的WPF Datagrid不显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!