IronPython& WPF:将复选框的IsChecked属性绑定到类成员变量

时间:2010-10-04 15:55:59

标签: python wpf visual-studio-2010 binding ironpython

我已经看到很多关于如何使用复选框进行数据绑定的类似问题,但我见过的示例中的全部都在C#中,我似乎无法实现飞跃将其转换为IronPython。我在窗口中定义了一个复选框:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="Test" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <DockPanel>     
        <CheckBox Name="bindtest" IsChecked="{Binding Path=o1checked, Mode=OneWay}"></CheckBox>
        <Button Content="Toggle" Name="Toggle" Padding="5"></Button>
    </DockPanel>
</Window>

我希望它的IsChecked值在以下类中切换self.o1checked时自动更新:

class MaikoCu64(object):
    def __init__(self):
        self.o1checked = False
        ui.win['Button']['Toggle'].Click += self.Toggle_OnClick

    def Toggle_OnClick(self, sender, event):
        self.o1checked = not self.o1checked

ui对象是一个将xaml加载到ui控件中的类。请参阅here

那么我该如何实现呢?通过MSDN绑定文档(也是所有在C#中)阅读了几个小时后,我尝试添加这个:

import System
myBinding = System.Windows.Data.Binding("o1checked")
myBinding.Source = self
myBinding.Mode = System.Windows.Data.BindingMode.OneWay
ui.win['CheckBox']['bindtest'].SetBinding(System.Windows.Controls.CheckBox.IsCheckedProperty, myBinding)

它不起作用,但它似乎至少有些意义。我是在正确的轨道上吗?

1 个答案:

答案 0 :(得分:3)

该属性应使用INotifyPropertyChanged接口。有关如何在IronPython中实现它的示例,请参阅我的blog

另请注意,.NET或IronPython中有一个Silverlight bug,当除了字符串以外的任何内容应该传播回viewmodel时会导致错误。