无法将ObservableCollection绑定到DataGrid

时间:2013-11-12 10:39:15

标签: c# wpf xaml datagrid itemssource

如果我用C#编写代码就可以了 比如

DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = true;
dg.ItemsSource = db;

其中db是

public ObservableCollection<Data> db = new ObservableCollection<Data>();

db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });

它会生成列和数据正常.. 但如果我在XAML中写道,它就无法显示任何内容

<DataGrid ItemsSource="{Binding db}" AutoGenerateColumns="True"/>

我找不到将此集合绑定到DataGrid的方法.. 请告诉我原因

这是我所有的xaml

<Window x:Class="testt.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>
    <StackPanel Orientation="Vertical" Name="Panel">
        <TextBlock Name="count"/>
        <DataGrid ItemsSource="{Binding db}" AutoGenerateColumns="True"/>       
    </StackPanel>
</Grid>
</Window>

谢谢

6 个答案:

答案 0 :(得分:2)

如果您不使用MVVM,请在代码中尝试此操作

DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = true;
dg.ItemsSource = db;
this.DataContext = this; //or put this line in constructor.

其中db是

public ObservableCollection<Data> db = new ObservableCollection<Data>();

db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });

在Xaml,

<Window x:Class="testt.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>
    <StackPanel Orientation="Vertical" Name="Panel">
        <TextBlock Name="count"/>
        <DataGrid ItemsSource="{Binding db}" AutoGenerateColumns="True"/>       
    </StackPanel>
</Grid>
</Window>

答案 1 :(得分:0)

好的,我认为你错过了最重要的部分。如果您要使用Binding,则必须在视图的某处设置DataContextThisBinding理论的良好开端。

通常,您会使用ViewModel。 E.g。

public class WndViewModel : ViewModelBase // the base class implementing INotifyPropertyChanged
                                          // although you don't need it for your case 
{
    private ObservableCollection<Data> _db;
    public ObservableCollection<Data> Db 
    { 
        get { return _db ?? (_db = new ObservableCollection<Data>()); 
    }

    public WndViewModel()
    {
        Db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
        Db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });
    }
}

然后你可以像这样使用ViewModel

<Window ...>
    <Window.DataContext>
         <someNs:WndViewModel />
    </Window.DataContext>
    <Grid>
        <StackPanel Orientation="Vertical" Name="Panel">
            <TextBlock Name="count"/>
            <DataGrid ItemsSource="{Binding Db}" AutoGenerateColumns="True"/>       
        </StackPanel>
    </Grid>
</Window> 

请注意,此代码仅用于演示目的,需要进行改进以获得良好的解决方案。

我还建议你熟悉MVVM模式。您可以先阅读this文章。

答案 2 :(得分:0)

您必须设置窗口的datacontext以将集合与其下的控件绑定。

ObservableCollection<Data> db = new ObservableCollection<Data>();

 db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
 db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });

 this.DataContext = db;

现在在XAML中你只设置了itemsource的绑定。

<DataGrid Name="grdPerson" ItemsSource="{Binding}" AutoGenerateColumns="True"></DataGrid>

答案 3 :(得分:0)

尝试为DataGrid设置“DisplayMemberPath”,这是设置DataSource后应该做的第一件事。

答案 4 :(得分:0)

您必须实施INotifyPropertyChanged接口才能反映更改。请参阅以下代码段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace StylingIntroSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
    private ObservableCollection<Data> _db = new ObservableCollection<Data>();

    public ObservableCollection<Data> db {
        get { return _db; }
        set
        {
            _db = value;
            OnPropertyChanged("db");
        }
    }

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

    private void LoadItems()
    {
        db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
        db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string PropertyName)
    {
        if (PropertyChanged!= null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
}

public class Data
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int Price { get; set; }
}

}

在XAML中

<Grid>
    <StackPanel Orientation="Vertical"
                Name="Panel">
        <TextBlock Name="count" />
        <DataGrid Name="MyGrid" ItemsSource="{Binding db}"
                  AutoGenerateColumns="True" />
    </StackPanel>
</Grid>

答案 5 :(得分:0)

感谢您的帮助 现在问题解决了!

db应该是属性,而不是字段,所以我将一些代码更改为

public ObservableCollection<Data> db { set; get; }

我必须用这个sentense设置DataContext

this.DataContext = this;

现在它可以正常工作:)