== Introduction == This page arose out of a very frustrating few days spent on trying to specify a derived class in an XRC document. Specifically, what I wanted to do was to create my own subclass of `wx.ListCtrl`, note in the XRC document that it should use this derived class, and have it be automatically instantiated for me. For the purposes of this example, the subclass is very simple. The only way it differs from the `wx.ListCtrl` class is that at instantiation time it adds a single column to itself. This subclass-specific code is contained in the `_PostInit()` method (the name `_PostInit` seems to be a mild convention for such code in TwoStageCreation classes). == The Solution == Here's what finally worked. {{{ #!python # MyList.py import wx, wx.xrc gui_xrc = """ List Test wxVERTICAL wxEXPAND """ class MyApp(wx.App): def OnInit(self): self.res = wx.xrc.EmptyXmlResource() self.res.LoadFromString(gui_xrc) self.frame = self.res.LoadFrame(None, 'frame') self.list = wx.xrc.XRCCTRL(self.frame, 'list') self.frame.Show() return True class cl(wx.ListCtrl): _firstEventType = wx.EVT_SIZE #_firstEventType = wx.EVT_WINDOW_CREATE def __init__(self): p = wx.PreListCtrl() # the Create step is done by XRC. self.PostCreate(p) # Apparently the official way to do this is: #self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate) # But this seems to be the actually working way, cf: # http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/2169189 self.Bind(self._firstEventType, self.OnCreate) def _PostInit(self): self.InsertColumn(0, "Test") def OnCreate(self, evt): self.Unbind(self._firstEventType) # Called at window creation time self._PostInit() self.Refresh() if __name__ == '__main__': app = MyApp() app.MainLoop() }}} == Notes == The main frustration comes from the fact that instantiation of objects from XRC files uses TwoStageCreation. If you were just creating the objects directly in Python code, the usual form of constructor would work: {{{ #!python def __init__(self, parent, ID, size, style): wx.ListCtrl.__init__(self, parent, ID, size, style) # Derived-class specific initialization }}} ...but since the object is being loaded from XRC, it is instantiated with no arguments. The XRC component will then call Create when the object is placed. === wx.EVT_WINDOW_CREATE === The official solution seems to be to bind a handler to `wx.EVT_WINDOW_CREATE`. This event, in theory, is passed at the time the window is placed and ready to go. However, I couldn't seem to get this to work. For some reason the event never seemed to arrive, so the _!PostInit() method of my derived class was never called. (I'm not sure why this is and would be grateful if someone could tell me.) And yet the !XmlSubclass example in the wxPython 2.5.3.1 demo binds to `wx.EVT_WINDOW_CREATE` and it seems to work fine. At any rate, I've used this [[http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/2169189|trick from the wxPython-users list]] instead. The first event the window gets is a `wx.EVT_SIZE` event, so at instantiation time we bind to that, and then when we get it, we immediately unbind from the size event (otherwise we'd be initializing every time the window was resized) and call `_PostInit()`. == In Hindsight == The actual !ListCtrl I was working on was more complex than this, but sometime after I finally got it working I began to doubt whether I needed to subclass !ListCtrl at all. What I was really looking for was a much higher-level interface to a !ListCtrl, so that I could do things like `myListCtrlInstance.AddMyComplexObjectAsASingleRow(complexObject)`. With that in mind, my new plan is to use a plain-vanilla `wx.ListCtrl` as a component in a higher-level object (probably called !ListManager or something). {{{ #!python class myApp(wx.App): def OnInit(self): # [ ... ] vanilla_list = wx.xrc.XRCCTRL(self.frame, 'list') self.listmgr = ListManager(vanilla_list, ...) def OnHandleSomeGoofyAction(self, evt): cplx = self.DoSomeRidiculousTransaction() self.listmgr.AddRow(cplx) }}}