如何强制列表视图将其复选框绘制为 Windows 表单中的单选按钮?

How can I force a listview to draw its checkboxes as radio buttons in Windows Forms?

我有一个 ListView,它表现得 完全符合我的要求,我显示复选框并且我只能select 一个项目一次。现在的问题是这是糟糕的用户体验——当一次只允许 1 selection 时,它不应该使用复选框,它应该使用单选按钮。所以我需要用单选按钮替换复选框。我认为最简单的方法是抢先绘制复选框并将其替换为单选按钮字形,但我不太确定如何从那里开始。此外,它需要在 ListView 上运行,而不是 ListBox,并且它需要在 WinForms 上运行,而不是 ASP 或 WPF。如果您能 link 到 documentation/code 显示绘图如何特别适用于 ListView,我将非常感谢您了解这一点的信息,那将是非常棒的。我找到了 .NET 4.8 的文档 here 并正在仔细阅读,但仍将不胜感激。

所以我的问题是,“我怎样才能将 ListView(不是 ListBox)子类化以使用单选按钮而不是复选框?”

如果 ListView.CheckBoxes 属性 是 true,您可以使用 ListView.StateImageList 属性 作为 checked/unchecked 项目图像的来源.

Remarks section 说:

The StateImageList property allows you to specify an ImageList that contains images to use to represent an application-specific state of an item in a ListView control. State images are displayed to the left of an icon for the item. You can use state images, such as checked and unchecked check boxes, to indicate application-defined item states. State images are visible in all views of the ListView control.

If an ImageList is specified in the StateImageList property and the CheckBoxes property is set to true, the images at index positions 0 and 1 in the ImageList are displayed instead of the check box. The image at index position 0 is displayed instead of the unchecked check box, and the image at index position 1 is displayed instead of the checked check box.