假设我有一个数据网格,其中 itemssource 绑定到属性 Collection,例如 IEnumerable。当然,我为其编写了适当的 getter 和 setter。
现在,当我分配给这个属性(集合)时,只是 IEnumerable (作为某种方法的结果),例如:
Collection = FooMethod(); // FooMethod returns IEnumerable<MyClass>
数据网格将显示空行。行数将与集合的计数相匹配。
但是当我强制转换时,如下所示:
Collection = FooMethodp().ToArray(); // forced fetching data
数据网格现在将显示所有包含内容的行。
那么,在纯 IEnumerable 的情况下,是什么阻止 datagrid 显示数据呢?它必须迭代集合,因此无论如何都会发生获取。
edits
只是为了记录。我的班级是这样的:
public class ErrorsIndicators
{
public double Min { get; set; }
public double Max { get; set; }
public double Avg { get; set; }
}
FooMethod 返回(返回产量)几个项目。所以,这里没什么特别的。
没有看到实施情况很难说FooMethod()
,但我怀疑它正在返回实现的东西IEnumerable<T>
但没有进一步的内容(例如ICollection<T>
or IList<T>
)。在这种情况下,DataGrid 似乎无法动态确定列名称,您需要通过DataGrid.Columns
财产。
这是我整理的一个简单的复制品。
MainWindow.xaml.cs:
namespace DataGridTest
{
using System.Collections.Generic;
using System.Windows;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Customers = this.GetCustomers();
DataContext = this;
}
private IEnumerable<Customer> GetCustomers()
{
yield return new Customer() { Name = "first" };
yield return new Customer() { Name = "second" };
yield return new Customer() { Name = "third" };
}
public IEnumerable<Customer> Customers
{
get;
set;
}
}
public class Customer
{
public string Name
{
get;
set;
}
}
}
主窗口.xaml:
<Window x:Class="DataGridTest.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">
<DataGrid ItemsSource="{Binding Customers}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</Window>
如果您删除DataGrid.Columns
从 XAML 来看,它显示三个空行。因此,当源仅实现时,似乎不是数据源的枚举被破坏IEnumerable<T>
,而是自动创建列。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)