myBlog

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

IronPythonで、動的に IValueConverter を使う

2011-07-04 14:43:57 | DataBinding
e-manual ⇒ WPF(データバインディング). ⇒ もっとも単純なデータバンディング
Slider コントロールの操作結果を TextBox コントロールに表示します。
次のコードは TextBox コントロールの Text プロパティに、
Slider コントロールの Value プロパティをバインドしています。
http://www.kanazawa-net.ne.jp/~pmansato/wpf/wpf_databinding.htm
を参考にしました。 C# から IronPython へ 変換し、動的要素を追加しました。

Ivalueconverter

#
# IValueConverter.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System import Object,String
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application
from System.Windows.Controls import TextBox
from System.Windows.Data import Binding, BindingOperations, IValueConverter

xaml_str="""
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Binding (IValueConverter)" Height="300" Width="300">
    <!-- 
        xmlns:c="clr-namespace:BindingTest" >
    <Window.Resources>
        <c:IntToHexValueConverter x:Key="valueConverter" />
    </Window.Resources>
    -->
    <StackPanel Orientation="Vertical">
        <!--
        <TextBox Name="textBox1" Width="100" Height="25" HorizontalAlignment="Left" Margin="30,10,0,0"
             Text="{Binding ElementName=slider1, Path=Value, Converter={StaticResource valueConverter}}"/>
        -->
        <TextBox Name="textBox1" Width="100" Height="25" HorizontalAlignment="Left" Margin="30,10,0,0"
             Text="{Binding ElementName=slider1, Path=Value, Converter={x:Null}}"/>
        <Slider Name="slider1" Height="22" Margin="30,20,60,0" Maximum="255" Minimum="0"
            TickPlacement="TopLeft" TickFrequency="16" SmallChange="1" LargeChange="16"
            IsSnapToTickEnabled="True" />
    </StackPanel>
</Window>
"""

class IntToHexValueConverter(IValueConverter):
    def Convert(self,value, targetType, parameter, culture):
        textValue = value
        return String.Format("{0} (0x{1:X2})", int(textValue), int(textValue) )

    def ConvertBack(self,value, TargetType, parameter, culture):
       return Binding.DoNothing
    #// end of IntToHexValueConverter class
 
class ExWindow(Object):
    def __init__(self):
        self.Root = win = XamlReader.Parse(xaml_str)
        self.textBox1 = win.FindName("textBox1")
        self.slider1 = win.FindName("slider1")
        #myBinding = BindingOperations.GetBinding(self.textBox1, TextBox.TextProperty)
        BindingOperations.ClearBinding(self.textBox1, TextBox.TextProperty)
        myBinding = Binding("Value")
        myBinding.Source = self.slider1
        myBinding.Converter = IntToHexValueConverter()
        myBinding.ConverterParameter = "Value"
        self.textBox1.SetBinding(TextBox.TextProperty, myBinding)

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

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

最新の画像もっと見る

コメントを投稿