Arvo Bowen Arvo Bowen - 1 year ago 148
C# Question

Why does OnDrawItem event for a ListView not affect the Design-time environment?

If I create a class and make it derive from a ListView like this...

class MyListView : ListView
public MyListView() : base()
DoubleBuffered = true;
OwnerDraw = true;
Cursor = Cursors.Hand;
Scrollable = false;

protected override void OnDrawItem(DrawListViewItemEventArgs e)

Then I open the design view of my windows form and add a new MyListView object then add a single item and link it to a image list. I can see that there is one item in the mylistview object. It has no effect on the object I have on my form called
of type
. When I run my app on the other hand I see exactly what I expected and there is no items listed.

Why would this effect run-time and not design-time painting?

enter image description here

Answer Source

ListViewDesigner shadows OwnerDraw property like Visible or Enabled property of control. So it just works at run-time and changing it doesn't affect design-time.

If you take a look at source code of ListViewDesigner, you will see this property:

private bool OwnerDraw
    get { return (bool) base.ShadowProperties["OwnerDraw"]; }
    set { base.ShadowProperties["OwnerDraw"] = value; }

And in PreFilterProperties you will see the designer replaced the original property with this one:

PropertyDescriptor oldPropertyDescriptor = (PropertyDescriptor) properties["OwnerDraw"];
if (oldPropertyDescriptor != null)
    properties["OwnerDraw"] = TypeDescriptor.CreateProperty(typeof(ListViewDesigner), 
        oldPropertyDescriptor, new Attribute[0]);

So it doesn't matter what View you use, it performs the default painting regardless of what you have in OnDrawItem. It's because it doesn't use OwnerDraw property at design-time. The designer shadows it. This is the same behavior which you see for Enabled or Visible property.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download