C# WPF datagrid:列数始终等于 0
我在 C# 中创建了一个 WPF takelit 数据网格,并在 XAML 中设置了 ItemsSource。 列是自动生成的。
我正在尝试做一个使用 ObservableCollection 的数据网格,非常类似于 网站。
在底部,您将找到一个可以下载的示例(这里是 link)
我的问题是我正在尝试更改特定列的某些属性(例如:Datagrid.Columns[index]。属性)但这似乎不起作用,因为列数始终等于 0。因此,我无法使用我想要使用的索引来更改属性,因为 Columns 集合中似乎没有任何内容。 然而在运行时会显示一些列。
那么,列计数怎么可能等于 0?
以及
在我的情况下如何更改特定列的属性?
I have created a WPF tooklit datagrid in C# and the ItemsSource is set in the XAML. The columns are automatically generated.
I am trying to do a datagrid that uses an ObservableCollection, pretty much like what is on this website.
At the bottom you will find a sample that you can download (here is the link)
My problem is that I'm trying to change certain properties of a specific column (example: Datagrid.Columns[index].Property) but this does not seem to work since the the column count is always equal to 0. Therefore, I cannot change the properties by using the index that I want to use because there seems to be nothing in the Columns collection. Yet at runtime there are columns that are displayed.
So, how can it be possible that column count is equal to 0?
and
How can I change the properties of a specific column in my situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据多个论坛的经验,如果列是自动生成的,则无法通过 Columns 属性访问它们。 我不知道为什么会这样,但以前就见过这样的情况。
According to several forum experiences, columns aren't accessible via the Columns property if they are autogenerated. I don't know why this is so, but it has been seen before.
我终于找到了方法! 列计数等于 0 的原因是因为尚未生成列。 为了更改属性,我必须使用 AutoGenerateColumns 事件并更改其中的属性。
I have finally found how! The reason that the column count equals 0 is because the columns are not yet generated. In order to change the properties I have to use the AutoGeneratedColumns event and change the properties in there.