I currently have a need for a custom ListViewItem class – let’s call it MyListViewItem. It needs to have some additional data associated with each item, and perform some operations when the Checked property is changed. I’ve tried several things, but currently the relevant code looks like this:
class MyListViewItem : ListViewItem {
new public bool Checked {
get {
return base.Checked;
}
set {
base.Checked = value;
// do some other things here based on value
}
}
public MyListViewItem(Object otherData) {
// ...
}
}
The problem I’m having is that when I click on the item’s checkbox in the ListView, my setter is never called. Does anyone know what I am doing wrong? I’m aware that I could use the ItemChecked event of the parent ListView, but that seems like a much less clean solution. (Also I’m not actually passing an Object to the constructor, but that part isn’t important here).
It’s not working cause the “new” keyword doesn’t override it just “hides”.
This means that if you call Checked on an instance of object that is referenced through the type definition of MyListViewItem you will run your code. However the ListView references to this object via the type definition of ListViewItem and therefore will not call your “new” method.
“new” is not override. The better solution is to probably handle the code in a custom list view. It isn’t really that ugly.