myBlog

IronPython, Silverlight, WPF, XAML, HTML5, ...

IronPythonで、Windowの背景をグラデーション表示 《 IMultiValueConverter 使用 》

2011-07-07 01:56:00 | DataBinding
IronPythonで、Windowの背景をグラデーション表示しました。MultiBinding を使って。
Blog: メモ ⇒ [WPF]LinearGradientBrushのGradientStopの色はBindingできない。
http://d.hatena.ne.jp/kn000/20100812/1281632098
を参考にしました。C# から IronPython へ変換しました。
色指定は、Colorの名前だけでなく、16進数でも入力できます。

Lineargradientvalueconverter_2

#
# LinearGradientValueConverter.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase') # for Point
from System import Object
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application, PropertyPath, Point
from System.Windows.Controls import TextBox
from System.Windows.Media import(
        Color,ColorConverter,LinearGradientBrush,GradientStop,Brushes )
from System.Windows.Data import( 
        Binding,BindingOperations, MultiBinding, IMultiValueConverter )

xaml_str="""
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LinearGradientValueConverter" Height="300" Width="320">
    <Canvas>  <!-- Text="#FFFFFFFF" or "White"  "#FF000000" or "Black" -->
        <TextBox x:Name="textBox1" Width ="80" Text="#FFFFFFFF" 
                 Canvas.Top = "5" Canvas.Left="8" />
        <TextBox x:Name="textBox2" Width ="80" Text="Black" 
                 Canvas.Top = "25" Canvas.Left="8" />
    </Canvas>
</Window>
"""
class LinearGradientValueConverter(IMultiValueConverter):
    def Convert(self, values, targetType, parameter, culture):
        try:
            color1 = ColorConverter.ConvertFromString(values[0])
            color2 = ColorConverter.ConvertFromString(values[1])
            brush = LinearGradientBrush(StartPoint=Point(0.5, 0.0),EndPoint=Point(0.5, 1.0))
            brush.GradientStops.Add( GradientStop(color1, 0.0) )
            brush.GradientStops.Add( GradientStop(color2, 1.0) )
            result = brush
        except:
            return None
        return result
    def ConvertBack(self, value, targetTypes, parameter, culture):
        return None

class ExWindow(Object):
    def __init__(self):
        self.Root = win = XamlReader.Parse(xaml_str)
        self.textBox1 = win.FindName("textBox1")
        self.textBox2 = win.FindName("textBox2")
        binding = MultiBinding()
        binding.Bindings.Add( Binding(Source = self.textBox1,
                                      Path = PropertyPath(TextBox.TextProperty.Name) ) )
        binding.Bindings.Add( Binding(Source = self.textBox2,
                                      Path = PropertyPath(TextBox.TextProperty.Name) ) )
        binding.Converter = LinearGradientValueConverter()
        #BindingOperations.SetBinding(win, win.BackgroundProperty, binding)
        win.SetBinding(win.BackgroundProperty, binding)

if __name__ == "__main__":
    win = ExWindow()
    Application().Run(win.Root)


IronPythonの世界 (Windows Script Programming)
荒井 省三
ソフトバンク クリエイティブ
エキスパートPythonプログラミング
Tarek Ziade
アスキー・メディアワークス
Pythonスタートブック
辻 真吾
技術評論社

最新の画像もっと見る

コメントを投稿