第2章 ListView 1/1《IronPython2.6》

記事一覧第1章第2章第3章第4章第5章第6章第7章余録A余録B余録C

C#.use(better, IronPython=”WPF”)
ListView

《著》本間りす《監修》小泉ひよ子とタマゴ倶楽部

■ 関連記事

ListView

ListView は、ListBox を拡張したコントロール ItemsControl で、複数の項目を管理します。各項目 ListViewItem は、コントロール ContentControl の一種で、単一の項目(ビジュアル要素)を格納します。

《Note》ListBox を拡張して、任意のコントロールを作成する方法については、ListView の実現方法を理解するのが早道です。詳細は、関連記事で紹介します。

事例:ListView

コントロール ListView は、リスト項目を表形式で表示したり、それらを並べ替えたいときに便利です。その便利な機能のいくつかを、次の例題で紹介します。



>ipy.exe exListView.py
'AntiqueWhite'(250,235,215)

アプリケーションを起動すると、リスト項目には(クラス Brushes で規定された)色の名前と色成分(左から順に赤/緑/青)を表示します。任意のリスト項目を選択すると、選択した色を表示するとともに、その情報をコマンドプロンプトにも出力します。ここでは、選択した項目 AntiqueWhite の色成分が 250,235,215 になっています。

リスト項目を追加する

ListView に、表示したいリスト項目を設定するとともに、各項目を選択したときの動作を規定します。

    def init(self):
        target = "listView", "colorBox"
        self._Controls(target)
        
        self.listView.ItemsSource = ObservableCollection[object]()
        for e in self.colorBrushes():
            self.listView.ItemsSource.Add(ColorItem(e))            
        self.listView.SelectionChanged += self.selectionChanged

複数のデータ項目を管理するのが、プロパティー ItemsSource です。これに、メソッド Add を使って追加した各項目 ColorItem(e) が、リストに表示されます。そして、イベント SelectionChanged に呼応する、イベントハンドラー selectionChanged を登録します。

イベントハンドラーを登録する

リスト項目を選択したときの動作を規定するのが、プロパティー SelectionChanged です。これには、次のようなイベントハンドラーを設定します。


   def selectionChanged(self, sender, e):
        e = sender.SelectedItem
        print ">>>", e
        self.colorBox.Background = getattr(Brushes, e.name) 

リスト項目を選択すると、イベント SelectionChanged に呼応して、イベントハンドラー selectionChanged を起動します。すると、選択した項目 SelectedItem の値 e をもとに、背景 Background を設定します。ここでは、色の名前 e.name をもとに、ブラシを選択します。たとえば、選択した項目が AntiqueWhite なら、Brushes.AntiqueWhite が得られます。

Last updated♪2009/07/06

《付録》exListView.xaml

<!--
#    Copyright (C) 2000-2008, (^.^) piyo-piyo Tamago-Club
#
# 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 
    • >
<DockPanel LastChildFill="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/sparkle"> <GridViewColumn Header="Color Name" DisplayMemberBinding="{Binding Path=name}" Width="90" /> <GridViewColumn Header="Red" DisplayMemberBinding="{Binding Path=red}" Width="45" /> <GridViewColumn Header="Green" DisplayMemberBinding="{Binding Path=green}" Width="45" /> <GridViewColumn Header="Blue" DisplayMemberBinding="{Binding Path=blue}" Width="45" />

《付録》exListView.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.Collections.ObjectModel import *
from System.Windows import *
from System.Windows.Media import *

## --------------------               
class ColorItem:
    def __init__(self, name):
        e = getattr(Brushes, name).Color
        self.name  = name
        self.red   = e.R
        self.green = e.G
        self.blue  = e.B

    def __str__(self):
        return "'%s'(%d,%d,%d)"%(
            self.name, self.red, self.green, self.blue)

## --------------------               
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 = "listView", "colorBox"
        self._Controls(target)
        
        self.listView.ItemsSource = ObservableCollection[object]()
        for e in self.colorBrushes():
            self.listView.ItemsSource.Add(ColorItem(e))            
        self.listView.SelectionChanged += self.selectionChanged

    def _Controls(self, target):
        controls = xaml_controls(self)
        for e in target:
            setattr(self, e, controls[e])        
        
    def colorBrushes(self):
        return [e for e in dir(Brushes)
            if isinstance(getattr(Brushes, e), SolidColorBrush)]
    
    def selectionChanged(self, sender, e):
        e = sender.SelectedItem
        print e
        self.colorBox.Background = getattr(Brushes, e.name)        

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

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