BLOG: Nine Works ==> 色を選択するComboBoxの作り方
http://nine-works.blog.ocn.ne.jp/blog/2011/01/combobox_f98e.html
を参考にさせていただきました。
C# のプログラムを IronPython に変換しました。
前回のカラー・コンボボックスは、Python の dir(Colors) を利用しました。
今回は、Type.GetProperties(Colors) を使いました。
( System.Reflection も使用 )
http://nine-works.blog.ocn.ne.jp/blog/2011/01/combobox_f98e.html
を参考にさせていただきました。
C# のプログラムを IronPython に変換しました。
前回のカラー・コンボボックスは、Python の dir(Colors) を利用しました。
今回は、Type.GetProperties(Colors) を使いました。
( System.Reflection も使用 )
#
# ColorComboBox_Reflection.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System.Windows.Markup import XamlReader
from System import Object, Type
from System.Windows import Window, Application, Thickness
from System.Windows.Controls import StackPanel, Button, TextBlock
from System.Windows.Media import Color, Colors, SolidColorBrush
xaml_str="""
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mscorlib="clr-namespace:System.Reflection;assembly=mscorlib"
Title="MainWindow" Height="168" Width="244">
<Window.Resources>
<DataTemplate x:Key="ColorBoxTemplate"
DataType="{x:Type mscorlib:PropertyInfo}">
<StackPanel Orientation="Horizontal" Margin="3">
<Border BorderBrush="Black" BorderThickness="2"
CornerRadius="5" Width="20" Height="20"
VerticalAlignment="Center"
Background="{Binding Path=Name}" />
<TextBlock Margin="5,0,5,0" Text="{Binding Path=Name}"
VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ComboBox Name="ColorBox" Height="35" Width="200" MaxDropDownHeight="210"
ItemTemplate="{StaticResource ColorBoxTemplate}"/>
</Grid>
</Window>
"""
class ExWindow(Object):
def __init__(self):
self.Root = win = XamlReader.Parse(xaml_str)
self.ColorBox = win.FindName("ColorBox")
self.ColorBox.Loaded += self.ColorBox_Loaded
self.ColorBox.SelectionChanged += self.ColorBox_SelectionChanged
def ColorBox_Loaded(self, sender, e):
#PropertyInfo[] colorProps = typeof(Colors).GetProperties();
colorProps = Type.GetProperties(Colors)
self.ColorBox.ItemsSource = colorProps
self.ColorBox.SelectedIndex = 0
def ColorBox_SelectionChanged(self, sender, e):
selected = self.ColorBox.SelectedItem
if (selected != None):
color = selected.GetValue(None, None)
self.Root.Background = SolidColorBrush(color)
if __name__ == "__main__":
win = ExWindow()
Application().Run(win.Root)
# ColorComboBox_Reflection.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System.Windows.Markup import XamlReader
from System import Object, Type
from System.Windows import Window, Application, Thickness
from System.Windows.Controls import StackPanel, Button, TextBlock
from System.Windows.Media import Color, Colors, SolidColorBrush
xaml_str="""
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mscorlib="clr-namespace:System.Reflection;assembly=mscorlib"
Title="MainWindow" Height="168" Width="244">
<Window.Resources>
<DataTemplate x:Key="ColorBoxTemplate"
DataType="{x:Type mscorlib:PropertyInfo}">
<StackPanel Orientation="Horizontal" Margin="3">
<Border BorderBrush="Black" BorderThickness="2"
CornerRadius="5" Width="20" Height="20"
VerticalAlignment="Center"
Background="{Binding Path=Name}" />
<TextBlock Margin="5,0,5,0" Text="{Binding Path=Name}"
VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ComboBox Name="ColorBox" Height="35" Width="200" MaxDropDownHeight="210"
ItemTemplate="{StaticResource ColorBoxTemplate}"/>
</Grid>
</Window>
"""
class ExWindow(Object):
def __init__(self):
self.Root = win = XamlReader.Parse(xaml_str)
self.ColorBox = win.FindName("ColorBox")
self.ColorBox.Loaded += self.ColorBox_Loaded
self.ColorBox.SelectionChanged += self.ColorBox_SelectionChanged
def ColorBox_Loaded(self, sender, e):
#PropertyInfo[] colorProps = typeof(Colors).GetProperties();
colorProps = Type.GetProperties(Colors)
self.ColorBox.ItemsSource = colorProps
self.ColorBox.SelectedIndex = 0
def ColorBox_SelectionChanged(self, sender, e):
selected = self.ColorBox.SelectedItem
if (selected != None):
color = selected.GetValue(None, None)
self.Root.Background = SolidColorBrush(color)
if __name__ == "__main__":
win = ExWindow()
Application().Run(win.Root)
![]() | IronPythonの世界 (Windows Script Programming) |
荒井 省三 | |
ソフトバンク クリエイティブ |
![]() | エキスパートPythonプログラミング |
Tarek Ziade | |
アスキー・メディアワークス |
![]() | Pythonスタートブック |
辻 真吾 | |
技術評論社 |
※コメント投稿者のブログIDはブログ作成者のみに通知されます