カラー・リストビュー ( ColorListView )を作ってみました。
BLOG: 続・ひよ子のきもち ==> ListView
http://d.hatena.ne.jp/kotsubu-chan/20090706
から、XAML と IronPython のコード を myBlog 用に少しアレンジしています。
XAMLのなかで、DataBinding が使われています。
続・ひよ子のきもち は、とても興味深いPageです。
WPF・IronPythonについて、色々と教えてもらいました。
BLOG: 続・ひよ子のきもち ==> ListView
http://d.hatena.ne.jp/kotsubu-chan/20090706
から、XAML と IronPython のコード を myBlog 用に少しアレンジしています。
XAMLのなかで、DataBinding が使われています。
続・ひよ子のきもち は、とても興味深いPageです。
WPF・IronPythonについて、色々と教えてもらいました。
#
# exListView.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System import Object
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application
from System.Windows.Media import SolidColorBrush, Brushes
#from System.Windows.Controls import Grid
#from System.Windows.Data import Binding, BindingOperations
from System.Collections.ObjectModel import ObservableCollection
xaml_str="""
<DockPanel LastChildFill="True"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView Name="listView" DockPanel.Dock="Left">
<ListView.View>
<GridView>
<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" />
</GridView>
</ListView.View>
</ListView>
<Canvas Name="colorBox" />
</DockPanel>
"""
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):
for key,value in args.items():
setattr(self, key, value)
self.InitializeComponent(Content)
self.init()
def InitializeComponent(self, Content):
self.Content = XamlReader.Parse(Content)
def init(self):
target = "listView", "colorBox"
for e in target:
control = self.Content.FindName(e)
setattr(self, e, control)
self.listView.ItemsSource = ObservableCollection[Object]()
for e in self.colorBrushes():
self.listView.ItemsSource.Add(ColorItem(e))
self.listView.SelectionChanged += self.selectionChanged
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__":
win = ExWindow(Title="exListView", Width=380, Height=150, Content=xaml_str)
Application().Run(win)
# exListView.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System import Object
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application
from System.Windows.Media import SolidColorBrush, Brushes
#from System.Windows.Controls import Grid
#from System.Windows.Data import Binding, BindingOperations
from System.Collections.ObjectModel import ObservableCollection
xaml_str="""
<DockPanel LastChildFill="True"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView Name="listView" DockPanel.Dock="Left">
<ListView.View>
<GridView>
<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" />
</GridView>
</ListView.View>
</ListView>
<Canvas Name="colorBox" />
</DockPanel>
"""
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):
for key,value in args.items():
setattr(self, key, value)
self.InitializeComponent(Content)
self.init()
def InitializeComponent(self, Content):
self.Content = XamlReader.Parse(Content)
def init(self):
target = "listView", "colorBox"
for e in target:
control = self.Content.FindName(e)
setattr(self, e, control)
self.listView.ItemsSource = ObservableCollection[Object]()
for e in self.colorBrushes():
self.listView.ItemsSource.Add(ColorItem(e))
self.listView.SelectionChanged += self.selectionChanged
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__":
win = ExWindow(Title="exListView", Width=380, Height=150, Content=xaml_str)
Application().Run(win)
![]() | IronPythonの世界 (Windows Script Programming) |
荒井 省三 | |
ソフトバンク クリエイティブ |
![]() | エキスパートPythonプログラミング |
Tarek Ziade | |
アスキー・メディアワークス |
![]() | Pythonスタートブック |
辻 真吾 | |
技術評論社 |