《付録》GroupControl.py

# -*- coding: utf-8 -*-
#===============================================================================
#    Copyright (C) 2000-2008, 小泉ひよ子とタマゴ倶楽部
#
# History: WPF examples
#    2008/01/25, IronPython 1.1.1 (download)
#    2008/08/22, IronPython 1.1.2 (download)
#    2008/03/16, ver.2.0, WPF
#    2008/00/00, ver.2.1, IronPython 1.1.2 
#===============================================================================
from _ant import *
from System import *
from System.Windows import *
from System.Windows.Controls import *
from System.Windows.Media import *
from System.Windows.Media.Imaging import *
from System.Windows.Shapes import *

## --------------------
class Team:
    def __iter__(self):
        m = self.teams()
        for e in "ABCDEFGH":
            yield e, m[e]

    def teams(self):
        return {
            "A": ("GER", "ECU", "POL", "CRC",), # Group A
            "B": ("ENG", "SWE", "PAR", "TRI",), # Group B
            "C": ("ARG", "NED", "CIV", "SCG",), # Group C
            "D": ("POR", "MEX", "ANG", "IRN",), # Group D
            "E": ("ITA", "GHA", "CZE", "USA",), # Group E
            "F": ("BRA", "AUS", "CRO", "JPN",), # Group F
            "G": ("SUI", "FRA", "KOR", "TOG",), # Group G
            "H": ("ESP", "UKR", "TUN", "KSA",), # Group H
            }

## --------------------               
class ExWindow(Window):
    def __init__(self, Content=None, **args):
        self.InitializeComponent(Content)
        self.init()

    def InitializeComponent(self, Content):
        self.Content = LoadXaml(Content)
        
    def init(self):
        target = "tabControl",
        self._Controls(target)
        for group, teams in Team():
            item = TabItem(
                Header=group,
                )
            self.tabControl.Items.Add(item)            
            panel = WrapPanel()
            item.Content = panel
            for e in teams:
                brush = ImageBrush(
                    ImageSource=BitmapImage(Uri("image/%s.gif"%e, UriKind.Relative))
                    )
                panel.Children.Add(Rectangle(
                    Width=50,
                    Height=50,
                    Fill=brush,
                    ))
        self.tabControl.SelectionChanged += self.selectionChanged

    def _Controls(self, target):
        controls = xaml_controls(self)
        for e in target:
            setattr(self, e, controls[e])

    def selectionChanged(self, sender, e):
        s = sender.SelectedItem
        print s.Header

## --------------------               
if __name__ == "__main__":
    xaml = __file__.replace(".py",".xaml")
    win = ExWindow(
        Title=xaml,
        Width=260, Height=120,
        Content=xaml,
        )
    Application().Run(win)

## --------------------