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

Edw*_*uay 8 c# 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 { FirstName = "Joe", LastName = "Thompson" });
            Customers.Add(new Customer { FirstName = "Jill", LastName = "Smith" });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*hnB 7

通常在WPF中,您可以使用 ObservableCollection<>

因为那时你可以只.Add()/ .Remove()/来自源集合的元素,并且任何绑定的控件(数据绑定)会自动更新(自动属性更改通知).这是WPF中的两个重要概念.

主窗口视图模型

using System.Collections.Generic;

namespace TestDatagrid345.ViewModels
{
  class Window1ViewModel
  {
    private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
    public ObservableCollection<Customer> Customers
    {
      Get { return _customers; }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

主窗口

using System.Collections.Generic;
using System.Windows;

namespace TestDatagrid345
{
  public partial class Window1 : Window
  {
    Window1ViewModel _viewModel;

    public Window1()
    {
      InitializeComponent();
      _viewModel = (Window1ViewModel)this.DataContext; // @#$% (see XAML)
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      // but this stuff could instead be done on a 'Submit' button click on form:
      _viewModel.Customers.Add(new Customer { FirstName = "Tom", LastName = "Jones" });
      _viewModel.Customers.Add(new Customer { FirstName = "Joe", LastName = "Thompson" });
      _viewModel.Customers.Add(new Customer { FirstName = "Jill", LastName = "Smith" });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

主窗口XAML

<Window
  x:Class="TestDatagrid345.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:vm="clr-namespace:TestDatagrid345.ViewModels"
  Title="Window1"
  Height="350"
  Width="525"
  WindowState="Maximized">
  <Window.DataContext>
    <vm:Window1ViewModel /> <!-- this needs to be here for @#$% -->
  </Window.DataContext>
  <Grid>
   <DataGrid
      AutoGenerateColumns="True"
      ItemsSource="{Binding Path=Customers}"
      AlternatingRowBackground="LightBlue"
      AlternationCount="2" />
  </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)


Arc*_*rus 1

现在从绑定中删除 Path=Customers,它应该可以工作:)

  • 您仍然需要绑定...像这样:ItemsSource={Binding} (2认同)