[英]WPF DataGrid Take() not working for ItemsSource
I am trying to bind a DataGrid to an Array for testing purposes. 我正在尝试将DataGrid绑定到数组以进行测试。 As long as I am not trying to filter anything, the auto columns work nicely. 只要我不尝试过滤任何内容,自动列就可以很好地工作。
As soon as I try to filter the array by .Take(5) or any other filter, the rows stay empty, and there are only thing horizontal lines. 一旦我尝试通过.Take(5)或任何其他过滤器过滤数组,行将保持为空,并且只有水平线。 I think it may have something to do, with the "anonymous" class generated by the Take. 我认为这可能与Take生成的“匿名”类有关。 But this is a wild guess... 但这是一个疯狂的猜测...
Let me show you some code which works nicely, and does what I want: 让我向您展示一些效果很好的代码,它可以实现我想要的功能:
public partial class WindowLister : UserControl
{
private int counter = 0;
public WindowLister()
{
InitializeComponent();
dataGrid1.ItemsSource = SystemWindow.FilterToplevelWindows(filterFunction);
}
private bool filterFunction(SystemWindow window)
{
counter++;
if (counter > 5) return false;
return true;
}
}
And now the version which does not work: 现在该版本不起作用:
public partial class WindowLister : UserControl
{
public WindowLister()
{
InitializeComponent();
dataGrid1.ItemsSource = SystemWindow.FilterToplevelWindows(filterFunction).Take(5);
}
private bool filterFunction(SystemWindow window)
{
return true;
}
}
For anyone interested, the Source used is from the very nice Lib: ManagedWinapi.Windows; 对于任何感兴趣的人,使用的源代码都来自非常好的Lib:ManagedWinapi.Windows;。
Any help is appreciated... Chris 任何帮助表示赞赏...克里斯
I expect you need a list ( Take
/ Where
etc will give you an IEnumerable<T>
/ IQueryable<T>
sequence). 我希望您需要一个列表( Take
/ Where
等会给您IEnumerable<T>
/ IQueryable<T>
序列)。 Try using .Take(5).ToList()
, or .Where(...).ToList()
etc (where ...
is your filter). 尝试使用.Take(5).ToList()
或.Where(...).ToList()
等(其中...
是您的过滤器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.