为什么我的 WPF Datagrid 不显示数据? [英] Why isn't my WPF Datagrid showing data?

查看:24
本文介绍了为什么我的 WPF Datagrid 不显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个walkthrough 说你可以创建一行中的 WPF 数据网格,但没有给出完整示例.

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.

我需要对下面的代码进行哪些更改才能使其在数据网格中显示数据?

What do I need to change on the code below to get it to show data in the 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>

背后的代码:

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 不显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆