【问题标题】:Why isn't my WPF Datagrid showing data?为什么我的 WPF Datagrid 不显示数据?
【发布时间】:2010-10-14 22:45:15
【问题描述】:

walkthrough 说您可以在一行中创建 WPF 数据网格,但没有给出完整的示例。

所以我使用通用列表创建了一个示例并将其连接到 WPF 数据网格,但它没有显示任何数据。

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

答案:

此代码现在可以使用:

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" });
        }
    }
}

【问题讨论】:

  • 只需将 DataContext = Customers 移动到您实际调用 Customers.Add 的位置下方!您正在使用 List 现在有更改通知!如果您确实需要更改通知,请使用 ObservableCollection

标签: c# wpf datagrid


【解决方案1】:

我试图弄清楚为什么 JohnB 的答案给出的相同代码对我不起作用,问题是模型对象(客户)没有属性,而是字段。将它们转换为属性解决了我的问题。

【讨论】:

    【解决方案2】:

    通常在 WPF 中,您会利用 ObservableCollection&lt;&gt;

    因为这样你就可以将.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; }
        }
      }
    }
    

    主窗口

    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" });
        }
      }
    }
    

    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>
    

    【讨论】:

      【解决方案3】:

      试试看: 填充您的 _customers 列表并分配属性 ItemsSource

              dataGrid1.ItemsSource = _customers;
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 不,还是不行,我把上面的当前代码重新贴出来了。
        • 你仍然需要绑定...像这样:ItemsSource={Binding}
        【解决方案5】:

        你需要添加

        DataContext = Customers;
        

        在你的 Window_Loaded() 中

        【讨论】:

        • 我将 DataContext = Customers 添加到 Window_Loaded() 但数据网格仍然不显示任何数据。我很确定我之前已经使用通用列表完成了此绑定,并且我认为 Datagrid 只是读取了通用对象的字段。我使用的是 2009 年 3 月版的工具包。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 2019-03-26
        相关资源
        最近更新 更多