WPF Metaclass

If you prefer to write an WPF application in a class, this is a metaclass that makes it easy to access controls, somewhat like waddle in XAML_GUI_Events_Example. Look at the code and comments at the bottom to see what it does.

Here's the metaclass code (and let's say you save it as WPFUIElements.py) import clr clr.AddReference("PresentationFramework") clr.AddReference("PresentationCore")

from System.Windows import UIElement, LogicalTreeHelper

class WPFClass(type): def __new__(meta,classname,bases,classDict): def __gettreechildren__(control,classDict): for item in LogicalTreeHelper.GetChildren(control): if hasattr(item,"Name"): if item.Name: classDict[item.Name] = classDict["_xaml"].FindName(item.Name) if isinstance(item, UIElement): classDict = __gettreechildren__(item,classDict) return classDict if "_xaml" in classDict.keys: classDict = __gettreechildren__(classDict["_xaml"],classDict) return type.__new__(meta,classname,bases,classDict) else: raise Exception("""Please define a _xaml attribute, example: class Main(Application):   __metaclass__ = WPFClass    _xaml = XamlReader.Load(XmlReader.Create(FileStream("main.xaml",FileMode.Open)))""")

Now here's the xaml (saving it as main.xaml, in the same folder):       

        

Now here's the main file, in the same folder as the other two files: import clr clr.AddReference("PresentationFramework") clr.AddReference("PresentationCore") clr.AddReference("System.Xml")

from System.Windows import Application from System.Windows.Markup import XamlReader from System.IO import FileStream, FileMode from System.Xml import XmlReader

from WPFUIElements import WPFClass

class Main(Application): __metaclass__ = WPFClass _xaml = XamlReader.Load(XmlReader.Create(FileStream("main.xaml",FileMode.Open))) def __init__(self): pass def run(self): self.open_menuitem.Header = "Open File" #instead of self._xaml.FindName("open_menuitem").Header = "Open File" self.new_menuitem.Header = "New File" #instead of self._xaml.FindName("new_menuitem").Header = "New File" self.Run(self._xaml)

Main.run

This is made obsolete in IronPython 2.7A, where there is clr.LoadComponent to do the same thing and more.