《付録》routedCommandToolBar.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.Input import *
from System.Windows.Media import *
from System.Windows.Media.Imaging import *
from System.Windows.Shapes import *

clr.AddReference("System.Windows.Forms")
from              System.Windows.Forms import OpenFileDialog

## --------------------             # Command::ConcreteCommand
class FileOpen(RoutedCommand):
    def __init__(self, panel):
        self.panel = panel
    def Execute(self, sender, e):
        dialog = OpenFileDialog()
        dialog.ShowDialog()
        e = dialog.FileName.split("\\")[-1]
        brush = ImageBrush(
            ImageSource=BitmapImage(Uri("Flags/%s"%e, UriKind.Relative))
            )
        self.panel.Children.Add(Rectangle(
            Width=50,
            Height=50,
            Fill=brush,
            ))
    def CanExecute(self, sender, e):
        e.CanExecute = True

## --------------------             # Command::ConcreteCommand
class FileClose(RoutedCommand):
    def __init__(self, window):
        self.window = window
    def Execute(self, sender, e):
        self.window.Close()
    def CanExecute(self, sender, e):
        e.CanExecute = True

## --------------------             # Command::ConcreteCommand
class PaintColor(RoutedCommand):
    def __init__(self, panel):
        self.panel = panel
    def Execute(self, sender, e):
        e = sender.Content
        self.panel.Background = getattr(Brushes, e)
    def CanExecute(self, sender, e):
        e.CanExecute = True

## --------------------             # Command::Client
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 = "toolBarTray", "panel",
        self._Controls(target)
        target = "fileToolBar", "colorToolBar",
        for e in self.toolBarTray.ToolBars:
            if e.Name in target:
                setattr(self, e.Name, e)
        for content, command, toolBar in [
            ("Open...", FileOpen  (panel=self.panel), self.fileToolBar ),
            ("Close"  , FileClose (window=self)     , self.fileToolBar ),
            ("Red"    , PaintColor(panel=self.panel), self.colorToolBar),
            ("Green"  , PaintColor(panel=self.panel), self.colorToolBar),
            ("Blue"   , PaintColor(panel=self.panel), self.colorToolBar),
            ]:
            button = Button(
                Content=content,
                Command=command,
                )
            binding = CommandBinding(
                command,
                command.Execute,
                command.CanExecute)
            button.CommandBindings.Add(binding)
            toolBar.Items.Add(button)
    def _Controls(self, target):
        controls = xaml_controls(self)
        for e in target:
            setattr(self, e, controls[e])

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

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