【问题标题】:Simple silverlight sample does not show my data on datagrid简单的 silverlight 示例未在 datagrid 上显示我的数据
【发布时间】:2010-06-29 14:47:05
【问题描述】:

我有一个简单的程序,它在数据网格上显示一个集合。当我运行代码时,我看不到显示数据的网格。型号是

public class Person
{
    public string Name;
    public int Age;
    public bool Sex;
}

视图模型是

public class PeopleViewModel:INotifyPropertyChanged
{
    List<Person> _personList;

    public PeopleViewModel()
    {
        _personList = new List<Person>();
        _personList.Add(new Person() { Name = "n1", Age = 20, Sex = true });
        _personList.Add(new Person() { Name = "n2", Age = 20, Sex = true });
        _personList.Add(new Person() { Name = "n3", Age = 20, Sex = true });
        _personList.Add(new Person() { Name = "n4", Age = 20, Sex = true });
        _personList.Add(new Person() { Name = "n5", Age = 20, Sex = false });
    }

    public List<Person> PersonList
    {
        get { return _personList; }
        set { _personList = value; }
    }


    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
}

视图 xaml

<Grid x:Name="LayoutRoot" Background="White">
    <sdk:DataGrid Name="dataGrid1">
    </sdk:DataGrid>
</Grid>

后面的代码是

public PeopleView()
    {
        InitializeComponent();
        PeopleViewModel model = new PeopleViewModel();
        dataGrid1.ItemsSource = model.PersonList;
    }

【问题讨论】:

  • 你应该考虑改变的两件事......让你的 List 成为 ObservableCollection,让你的 PersonList 成员使用 PropertyChange
  • @Muad'Dib - 你应该把你的评论作为答案......因为它是唯一正确的。

标签: data-binding datagridview silverlight-4.0


【解决方案1】:

您的 DataGrid XAML 是否应该包含 AutoGenerateColumns="True"? 我正在将您的代码与类似的detailed example 进行比较。

【讨论】:

    【解决方案2】:

    我绑定的集合不是公开的。那是我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-02
      • 2012-05-07
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多