相关疑难解决方法(0)

为什么我的WPF Datagrid不显示数据?

演练说您可以在一行中创建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)

c# wpf datagrid

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

datagrid ×1

wpf ×1