简体   繁体   English

ListView ColumnWidthChanged事件被“无故”触发

[英]ListView ColumnWidthChanged Event gets fired 'without reason'

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. 我有一个listview和一个ColumnWidthChanged事件的事件处理程序,每次用户更改某些内容时,该事件处理程序都会在配置文件中存储新的列大小。

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 ). 仍然会在程序启动时触发此事件(对于listview中的每一列一次)。

The call stack only shows the event, 'external code' and the main 'program.cs'. 调用堆栈仅显示事件,“外部代码”和主“ program.cs”。

I just can't find out where the event is getting triggered from. 我只是找不到事件从何处触发。 Any ideas? 有任何想法吗?

You will need to put the code where you wire up the ColumnWidthChanged event into the Shown event of your form. 您将需要在将ColumnWidthChanged事件连接到表单的Shown事件中的地方放置代码。

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. 发生的情况是ListView占用了表单上的物理空间,因此在构建表单时,它必须计算每列的大小。 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. 这就是为什么它在显示之前为每列触发一次。

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. 至于为什么它不在DataGridView上触发,我认为对象的大小就是添加到表单中的大小。 It would wait to calculate the size of each of the columns until after the DataGridView is already added to the Form. 它将等待计算每列的大小,直到将DataGridView添加到窗体之后。 It's a little unintuitive behavior, but that appears to be what is happening from the results you're seeing. 这是有点不直观的行为,但这似乎就是您所看到的结果所发生的事情。

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

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