【问题标题】:Why DataGrid in WPF 4.0 doesn't show Data?为什么 WPF 4.0 中的 DataGrid 不显示数据?
【发布时间】:2010-06-28 18:21:22
【问题描述】:

所以我得到了一个 ObservableCollection 的 tmplist,其中我只有很少的元素。

我将它连接到 WPF 4.0 中 DataGrid 的 ItemsSource 并且它没有显示任何内容。不添加任何列。如果我添加断点,我可以看到“ItemsSource 计数等于 tmplist 元素的数量,但 Columns 计数等于 0。这是为什么?这是一些代码:

private ObservableCollection<Products> tmplist = new ObservableCollection<Products>(); 
public Products_view()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Products_view_Loaded);
    }

void Produkty_widok_Loaded(object sender, RoutedEventArgs e)
    {
        grid.ItemsSource = tmplist;

    }

和 xaml:

  <DataGrid Name="grid" CurrentCellChanged="grid_CurrentCellChanged" CellEditEnding="grid_CellEditEnding" RowEditEnding="grid_RowEditEnding" AlternatingRowBackground="#FFA0D1EA" AlternationCount="1" Background="#FF64A0BE" RowBackground="White" />

【问题讨论】:

    标签: c# .net wpf datagrid wpfdatagrid


    【解决方案1】:

    tmplist 必须填充项目。 尝试在构造函数中添加一个项目以进行测试。

    【讨论】:

    • 但是 tmplist 已经填满了。我没有在代码中显示它,但是 tmplist 中填充了来自数据库的元素。
    • 为了好玩,请尝试将 tmplist 设为公共属性
    • 创建了一个示例,我按照您的示例进行操作。它工作得很好......??您在其中一个事件处理程序中做了什么花哨的事情吗?
    • hkon,你能上传一个带有那个例子的项目吗?
    【解决方案2】:

    检查数据网格的 AutoGenerateColumns 属性是否设置为 true。

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 2019-03-26
      • 2012-11-26
      相关资源
      最近更新 更多