为什么我的ListView不显示List或Details项?

时间:2022-08-26 22:48:48

Using C# .NET 2.0, I have an owner-drawn ListView where I'm overriding the OnDrawColumnHeader, OnDrawItem and OnDrawSubitem events. If I set the View property to Details at design-time, everything works beautifully and I can switch the View property and all view modes display as they should (I'm not using Tile view). However, if I start in any other View, both the List and Details views are blank.

使用C#.NET 2.0,我有一个所有者绘制的ListView,我将覆盖OnDrawColumnHeader,OnDrawItem和OnDrawSubitem事件。如果我在设计时将View属性设置为Details,那么一切都很好用,我可以切换View属性,所有视图模式都应该显示(我没有使用Tile视图)。但是,如果我从任何其他视图开始,则列表视图和详细信息视图都是空白的。

I know you'll probably want to see code, but there's a lot of it, so I'm hesitant to post that much, but can if necessary. I'm more curious if someone has seen this before, and/or might have an inkling of how to fix it. The View property will be a user-saved setting, so I won't always be able to start in Details view by default.

我知道你可能想要查看代码,但是有很多代码,所以我对发布这么多内容犹豫不决,但如果有必要的话可以。如果有人之前见过这个,我会更好奇,并且/或者可能对如何修复它有所了解。 View属性将是用户保存的设置,因此默认情况下我无法始终在Details视图中启动。

4 个解决方案

#1


2  

The WinForms ListView is mostly a layer of abstraction of the top of the actual Windows control, so there are aspect of its behaviour that are, well, counterintuitive is a polite way of putting things.

WinForms ListView主要是实际Windows控件顶层的一个抽象层,所以它的行为方面是,反直觉是一种礼貌的放置方式。

I have a vague recollection, from back in my days as a Delphi developer, that when you are Owner drawing a ListView, the subitems of the control aren't actually populated unless your Listview is in "Details" mode when you load the items.

在我作为Delphi开发人员的日子里,我有一个模糊的回忆,当你是所有者绘制ListView时,除非你的Listview在加载项目时处于“详细信息”模式,否则实际上不会填充控件的子项。

Things to try ...

要尝试的事情......

... force the WinForms control to recreate the underlying windows handle after you change the display style. If memory serves, DestroyHandle() is the method you want.

...强制WinForms控件在更改显示样式后重新创建基础窗口句柄。如果内存服务,DestroyHandle()是你想要的方法。

... assuming you have a "Refresh" in your application to reload the data, do things work properly when you refresh after changing the display style?

...假设您的应用程序中有一个“刷新”以重新加载数据,在更改显示样式后刷新时可以正常工作吗?

... if all else fails, beg borrow or steal a copy of Charles' Petzolds classic on windows programming.

...如果所有其他方法都失败了,请在Windows编程中借用或窃取Charles'Petzolds经典的副本。

#2


4  

Either SubItems are not added, or you didn't add any columns. That's my initial feeling.

未添加SubItems,或者您没有添加任何列。这是我最初的感受。

#3


0  

If you configure it correctly using the designer, just go into the generated designer code and see what code was emitted by Visual Studio to get it to work right. THen just emulate that code.

如果使用设计器正确配置它,只需进入生成的设计器代码,然后查看Visual Studio发出的代码,以使其正常工作。那只是模仿那段代码。

#4


0  

Withour your code, there is nothing much can be said but DrawColumnHeader is only called when OwnerDraw property is set to true. Not sure if it is automatically set to true and false depending on the View property but it is worth giving a try. So make sure OwnerDraw is set to true before launching your application.

对于你的代码,没有什么可以说,但DrawColumnHeader仅在OwnerDraw属性设置为true时被调用。根据View属性,不确定它是否自动设置为true和false,但值得尝试一下。因此,在启动应用程序之前,请确保将OwnerDraw设置为true。

#1


2  

The WinForms ListView is mostly a layer of abstraction of the top of the actual Windows control, so there are aspect of its behaviour that are, well, counterintuitive is a polite way of putting things.

WinForms ListView主要是实际Windows控件顶层的一个抽象层,所以它的行为方面是,反直觉是一种礼貌的放置方式。

I have a vague recollection, from back in my days as a Delphi developer, that when you are Owner drawing a ListView, the subitems of the control aren't actually populated unless your Listview is in "Details" mode when you load the items.

在我作为Delphi开发人员的日子里,我有一个模糊的回忆,当你是所有者绘制ListView时,除非你的Listview在加载项目时处于“详细信息”模式,否则实际上不会填充控件的子项。

Things to try ...

要尝试的事情......

... force the WinForms control to recreate the underlying windows handle after you change the display style. If memory serves, DestroyHandle() is the method you want.

...强制WinForms控件在更改显示样式后重新创建基础窗口句柄。如果内存服务,DestroyHandle()是你想要的方法。

... assuming you have a "Refresh" in your application to reload the data, do things work properly when you refresh after changing the display style?

...假设您的应用程序中有一个“刷新”以重新加载数据,在更改显示样式后刷新时可以正常工作吗?

... if all else fails, beg borrow or steal a copy of Charles' Petzolds classic on windows programming.

...如果所有其他方法都失败了,请在Windows编程中借用或窃取Charles'Petzolds经典的副本。

#2


4  

Either SubItems are not added, or you didn't add any columns. That's my initial feeling.

未添加SubItems,或者您没有添加任何列。这是我最初的感受。

#3


0  

If you configure it correctly using the designer, just go into the generated designer code and see what code was emitted by Visual Studio to get it to work right. THen just emulate that code.

如果使用设计器正确配置它,只需进入生成的设计器代码,然后查看Visual Studio发出的代码,以使其正常工作。那只是模仿那段代码。

#4


0  

Withour your code, there is nothing much can be said but DrawColumnHeader is only called when OwnerDraw property is set to true. Not sure if it is automatically set to true and false depending on the View property but it is worth giving a try. So make sure OwnerDraw is set to true before launching your application.

对于你的代码,没有什么可以说,但DrawColumnHeader仅在OwnerDraw属性设置为true时被调用。根据View属性,不确定它是否自动设置为true和false,但值得尝试一下。因此,在启动应用程序之前,请确保将OwnerDraw设置为true。