Wpf DataGrid未显示绑定数据

时间:2015-09-25 14:08:30

标签: c# wpf xaml datagrid

我觉得我想做的事情非常简单明了,但从我所做的研究来看,看起来我做得对。我刚刚制作了一个vanilla WPF项目,设置实体框架,现在我正在尝试将我的员工集合(有数据)绑定到数据网格。

XAML:

<DataGrid Name="EmployeesView" CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" IsReadOnly="True" Margin="10,10,0,0">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
                    <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
                    <DataGridTextColumn Header="Phone Number" Binding="{Binding PhoneNumber}" />
                    <DataGridTextColumn Header="Pay Rate" Binding="{Binding PayPerHour}" />
                    <DataGridTextColumn Header="Hire Date" Binding="{Binding HireDate}" />
                </DataGrid.Columns>
            </DataGrid>

这是我的模特:

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
    public double PayPerHour { get; set; }
    public DateTime HireDate { get; set; }
}

这里我的代码背后(不使用MVVM)

private CompanyContext db = new CompanyContext();
public ObservableCollection<Employee> Employees = new ObservableCollection<Employee>();

public AddEmployee()
{
    RefreshGrid();
    InitializeComponent();
}

private void RefreshGrid()
{
    Employees = db.Employees.ToObservableCollection();
    EmployeesView = new DataGrid();
    EmployeesView.ItemsSource = Employees;
}

.ToObservableCollection是一种扩展方法,只是将ienumerable转换为ObservableCollection我还尝试使用List<>。出于某种原因,我必须致电EmployeesView = new DataGrid();其他明智的EmployeesView.ItemsSource = Employees;EmployeesView上提供空引用。那可能是我出错的地方?我在这里输了,有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子:

<Window x:Class="DatagridBInding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid ItemsSource="{Binding Items}"/>
</Grid>

代码隐藏:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private ObservableCollection<DataTableItem> _Items;

    public ObservableCollection<DataTableItem> Items
    {
        get { return _Items; }
        set
        {
            _Items = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Items"));
        }
    }


    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        Items = new ObservableCollection<DataTableItem>();

        Task.Factory.StartNew(() =>
        {
            Dispatcher.Invoke(() =>
            {
                for (int i = 0; i < 10; i++)
                    Items.Add(new DataTableItem() { Data = "Data " + i });
            }
        );
        });
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

项目类:

public class DataTableItem : INotifyPropertyChanged
{
    private string _Data;

    public string Data
    {
        get { return _Data; }
        set
        {
            _Data = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Data"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

据我所知,您尚未将您的员工定义为公共财产。看看我的例子并这样做。

关于Employee,检查我的DataTableItem和INotifyPropertyChanged的实现。

您还可以从其他线程添加项目,以防万一从数据库加载项目时需要此方案。

还有一件事,不要像那样刷新网格,但尝试我在上面的例子中做的方式:

Items = new ObservableCollection<DataTableItem>();

Items.Clear();

然后添加你的项目,异步与否,这就在你身上。