C# 为什么我的 WPF Datagrid 不显示数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/672923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why isn't my WPF Datagrid showing data?
提问by Edward Tanguay
This walkthroughsays 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.
因此,我使用通用列表创建了一个示例并将其连接到 WPF 数据网格,但它没有显示任何数据。
What do I need to change on the code below to get it to show data in the datagrid?
我需要对下面的代码进行哪些更改才能使其在数据网格中显示数据?
ANSWER:
回答:
This code works now:
此代码现在有效:
XAML:
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>
Code Behind:
背后的代码:
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" });
}
}
}
采纳答案by Arcturus
Now remove the Path=Customers from your binding, and it should work :)
现在从您的绑定中删除 Path=Customers,它应该可以工作:)
回答by rudigrobler
You need to add
你需要添加
DataContext = Customers;
In your Window_Loaded()
在你的 Window_Loaded()
回答by lepumin
try it: populate you _customers list and asign property ItemsSource
试试看:填充您的 _customers 列表并分配属性 ItemsSource
dataGrid1.ItemsSource = _customers;
回答by JohnB
Typically in WPF, you would leverage an ObservableCollection<>
通常在 WPF 中,您将利用 ObservableCollection<>
Because then you can just .Add()
/ .Remove()
elements to/from the source collection, and any controls bound (Data Binding)automatically get updated (Automatic Property Change Notification). Those are 2 important concepts in WPF.
因为那样你就可以只.Add()
/.Remove()
元素到/从源集合,并且任何绑定的控件(数据绑定)会自动更新(自动属性更改通知)。这是 WPF 中的两个重要概念。
Main Window View Model
主窗口视图模型
using System.Collections.Generic;
namespace TestDatagrid345.ViewModels
{
class Window1ViewModel
{
private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
public ObservableCollection<Customer> Customers
{
Get { return _customers; }
}
}
}
Main Window
主窗口
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" });
}
}
}
Main Window XAML
主窗口 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>
回答by Daniel Crha
I was trying to figure out why the identical code as given by JohnB's answer did not work for me, and the problem was that the model object (Customer) did not have properties, but fields. Converting them to properties fixed my problem.
我试图弄清楚为什么 JohnB 的回答给出的相同代码对我不起作用,问题是模型对象(客户)没有属性,但有字段。将它们转换为属性解决了我的问题。