GridView绑定不起作用

时间:2013-01-28 18:24:10

标签: c# wpf data-binding gridview mvvm

这是我的MainWindow.cs:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MultipleProjectViewModel();
    }    

我的ViewModel:

 public class MultipleProjectViewModel : ViewModelBase
{
    private ObservableCollection<Project> _projectCollection;
    public MultipleProjectViewModel()
    {
        ProjectCollection = new ObservableCollection<Project>();
        ProjectCollection.Add(new Project(){Name = "baba",Description = "lala"});
    }
    public ObservableCollection<Project> ProjectCollection
    {
        get { return this._projectCollection; }
        set
        {
            if(_projectCollection != value)
            {
                _projectCollection = value;
                RaisePropertyChanged(()=> ProjectCollection);
            }
        }
    }
}

我的模特:

 public class Project
{
    public string Name { get; set; } // Project Name
    public string Description { get; set; } // Project Description
}

我的观点:

        <ListView ItemsSource="ProjectCollection">
        <ListView.View>
            <GridView>

                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Path=Description}"/>
            </GridView>
        </ListView.View>
    </ListView>

那为什么绑定不起作用?我编译时只看到一个空白的GridView。

1 个答案:

答案 0 :(得分:1)

 <ListView ItemsSource="{Binding ProjectCollection}">