ListView ColumnWidthChanged 事件被“无故"触发 [英] ListView ColumnWidthChanged Event gets fired 'without reason'

查看:23
本文介绍了ListView ColumnWidthChanged 事件被“无故"触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图和一个用于 ColumnWidthChanged 事件的事件处理程序,它在用户每次更改某些内容时将新的列大小存储在配置文件中.

I have a listview and an event handler for the ColumnWidthChanged event, which stores the new column sizes in a config file every time the user changes something.

我为每个在内部更改列的进程创建了标志,以便仅对用户输入做出反应.

I've created flags for every process that is changing the columns internally for being able to only react on user inputs.

仍然会在程序启动时触发此事件(列表视图中的每一列都触发一次).

Still this event gets fired at program start ( once for every column in the listview ).

调用堆栈仅显示事件、外部代码"和主program.cs".

The call stack only shows the event, 'external code' and the main 'program.cs'.

我就是不知道从哪里触发事件.有什么想法吗?

I just can't find out where the event is getting triggered from. Any ideas?

推荐答案

您需要将连接 ColumnWidthChanged 事件的代码放入 显示表单事件.

You will need to put the code where you wire up the ColumnWidthChanged event into the Shown event of your form.

发生的事情是 ListView 占用了表单上的物理空间,因此在构建它时,它必须计算每列的大小.因此,当它向其中填充数据时,这些列一旦获得数据就会调整大小.这就是为什么它甚至在显示之前为每一列触发一次.

What is happening is that the ListView is taking up physical space on the form, so as it is being built, it has to calculate the size of each column. Therefore, as it is populating the data into them, those columns get resized once they have the data. That would be why it is firing once for each column before it is even displayed.

至于为什么它没有在 DataGridView 上触发,我认为对象的大小是添加到表单中的大小.它将等待计算每一列的大小,直到 DataGridView 已经添加到表单之后.这是一个有点不直观的行为,但从您所看到的结果来看,这似乎是正在发生的事情.

As for why it is not firing on the DataGridView, I am thinking that the size of the object is what is added to the form. It would wait to calculate the size of each of the columns until after the DataGridView is already added to the Form. It's a little unintuitive behavior, but that appears to be what is happening from the results you're seeing.

这篇关于ListView ColumnWidthChanged 事件被“无故"触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆