在 C# 中禁用 ListView,但仍显示当前选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling a ListView in C#, but still showing the current selection
提问by Blorgbeard is out
I have a ListView control, and I'm trying to figure out the easiest/best way to disallow changing the selected row(s), without hidingthe selected row(s).
我有一个 ListView 控件,我试图找出最简单/最好的方法来禁止更改所选行,而不隐藏所选行。
I know there's a HideSelection
property, but that only works when the ListView
is still enabled (but not focused). I need the selection to be viewable even when the ListView is disabled.
我知道有一个HideSelection
属性,但只有在ListView
仍然启用(但未聚焦)时才有效。即使禁用了 ListView,我也需要可以查看选择。
How can I implement this?
我该如何实施?
采纳答案by Peter Hession
You could also make the ListView ownerdraw. You then have complete control over how the items look whether they are selected or not or whether the ListView itself is enabled or not. The DrawListViewItemEventArgs provides a way to ask the ListView to draw individual parts of the item so you only have to draw the bits you're interested in. For example, you can draw the background of the item but leave it up to the ListView to draw the text.
您还可以制作 ListView ownerdraw。然后,您可以完全控制项目的外观,无论它们是否被选中,或者是否启用了 ListView 本身。DrawListViewItemEventArgs 提供了一种方法让 ListView 绘制项目的各个部分,因此您只需绘制您感兴趣的部分。例如,您可以绘制项目的背景,但将其留给 ListView 进行绘制文本。
回答by Nick Berardi
There are two options, change the selected rows disabled colors. Or change all the other rows to simulate they are disabled except for the selected one. The first option is obviously the easiest, and the second option obviously is going to need some extra protections.
有两个选项,更改选定行禁用的颜色。或者更改所有其他行以模拟它们被禁用,除了选定的行。第一个选项显然是最简单的,第二个选项显然需要一些额外的保护。
I have actually done the first option before and it works quite well. You just have to remember to change the colors back to the defaults in case another row is selected later on in the process.
我之前实际上已经完成了第一个选项,并且效果很好。您只需要记住将颜色更改回默认值,以防稍后在此过程中选择另一行。
回答by Mugunth
Implement SelectedIndexChanged and do this
实现 SelectedIndexChanged 并执行此操作
private void listViewABC_SelectedIndexChanged(object sender, EventArgs e)
{
listViewABC.SelectedItems.Clear();
}