列表视图具有三态复选框 [英] Listview with tri-state checkbox
问题描述
我有一个带有复选框的ListView(Windows窗体),这些复选框有三种状态:未选中,已检查和混合。
当我点击复选框时,首先显示已检查状态,然后是混合状态,最后是未选中状态。但是我想跳混合状态(先检查然后取消选中)。混合状态必须只能以编程方式设置。
有什么方法可以实现这种行为吗?
亲切的问候,
Filipe Marques
我的尝试:
我试图覆盖方法ItemChecked和MouseDown但我无法实现假装行为。
Hi,
I have a ListView (Windows form) with checkboxes and those checkboxes have three states: unchecked, checked and mixed.
When I click in checkbox, first appears the checked state, then the mixed and finally the unchecked. However I want jump the mixed state (first checked and then unchecked). The mixed state must be only set programmatically.
There is any way to achieve this behavior?
Kind regards,
Filipe Marques
What I have tried:
I had try to override the method ItemChecked and the MouseDown but I could not achieve the pretended behavior.
推荐答案
我找到了一个解决方案我的问题。
I figure out a solution for my problem.
protected override void OnMouseDown(MouseEventArgs e) {
ListViewItem item = this.GetItemAt(e.X, e.Y);
if (item != null) {
ListViewHitTestInfo loc = this.HitTest(e.Location);
// detect if the click was in state image area
if (loc.Location.Equals(ListViewHitTestLocations.StateImage)) {
if (item.StateImageIndex == 1) { // indeterminate
item.Checked = false;
item.StateImageIndex = 2; // unchecked
return;
}
}
}
base.OnMouseDown(e);
}
只要Checked值发生变化(通过键盘或鼠标),就会触发Checkbox Click事件处理程序,但是当值为以编程方式更改。
在事件处理程序中插入以下代码
The Checkbox Click event handler is fired whenever the Checked value changes (either via keyboard or mouse) but is not fired when the value is changed programatically.
Within the event handler insert the following code
if(this.checkBox.CheckState == CheckState.Indeterminate)
{
this.checkBox.Checked = false;
}
选中状态通过Checked / Indeterminate / Unchecked,因此每当状态不确定时,复选框都会被检查
亲切的问候
The checked state go throu Checked/Indeterminate/Unchecked, therefore whenever the state is going to indeterminate the checkbox was previously checked
Kind Regards
这篇关于列表视图具有三态复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!