数据绑定WPF + IRONPYTHON

时间:2015-04-17 05:13:38

标签: wpf binding ironpython

我想连接wpf代码和ironpython。但我不知道如何使用 xaml和amp;之间的数据绑定py文件。我该怎么办?

XAML:

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="WpfApplication9" Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding Path=time1()}" HorizontalAlignment="Left" Margin="10,117,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="272"/>
    </Grid>
</Window>  

IronPython:

import wpf
import time
from System.Windows import Application, Window
from time import localtime
class MyWindow(Window):

        def __init__(self):
        wpf.LoadComponent(self, 'test.xaml')
        self.TextBlock.Text = time1()

    def time1():
        now = time.localtime()
        setime = "%02d%02d%02d" % (now.tm_hour, now.tm_min, now.tm_sec)

        while 1:
            cnow = time.localtime()
            ntime = "%02d%02d%02d" % (cnow.tm_hour, cnow.tm_min, cnow.tm_sec)

            if int(ntime) != int(setime):
                realtime = "%02d:%02d:%02d" % (cnow.tm_hour, cnow.tm_min, cnow.tm_sec)

                print realtime
                setime = ntime
if __name__ == '__main__':
Application().Run(MyWindow())
time1()

1 个答案:

答案 0 :(得分:0)

你应该绑定agains属性:

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="WpfApplication9" Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding SomeMemer, Mode=TwoWay}" HorizontalAlignment="Left" Margin="10,117,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="272"/>
    </Grid>
</Window>  

IronPython的:

import wpf
import time
from System.Windows import Application, Window
from time import localtime
class MyWindow(Window):

    someMember = None   

    def __init__(self):
    wpf.LoadComponent(self, 'test.xaml')
    self.someMember = "Hello World"

    @property
    def SomeMember(self):
        return self.someMember 

    @psetter.SomeMember
    def SomeMember(self, value):
        self.someMember = value 


if __name__ == '__main__':
Application().Run(MyWindow())
time1()