简体   繁体   English

WPF DataGrid 的行数?

[英]Row Count for WPF DataGrid?

How to count or return the last index of a DataGrid row?如何计算或返回DataGrid行的最后一个索引? Apparently there's isn't a DataGrid.Count() property.显然没有DataGrid.Count()属性。 I searched everywhere and I can't find an answer.我到处搜索,但找不到答案。

I could create a variable and use i++ as a counter for Row count, but doesn't a DataGrid has a built-in solution?我可以创建一个变量并使用i++作为Row计数的计数器,但是DataGrid没有内置解决方案吗?

您可以使用DataGrid.Items.Count来获取项目的数字。

将计数存储在变量中

 int num=dataGrid.Items.Count

As TBohnen.jnr mentioned you should count the items in the bounded datasource. 正如TBohnen.jnr所提到的,你应该计算有界数据源中的项目。 You can retrieve the datasource by casting dataGrid.DataContext to the appropriate Type (List, IEnumerable, or any other Type) and call .Count on that object. 您可以通过将dataGrid.DataContext强制转换为适当的Type (List,IEnumerable或任何其他Type)并在该对象上调用.Count来检索数据源。 It shouldn't be very hard... 这应该不是很难......

Items.Count returns 1 when you Datagrid is editable.当 Datagrid 可编辑时,Items.Count 返回 1。 This don't work for me.这对我不起作用。

Try this:尝试这个:

int num=dataGrid.ItemsSource.OfType<object>().Count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM