【发布时间】:2010-10-04 15:55:59
【问题描述】:
我已经看到很多类似的问题,关于如何使用复选框进行数据绑定,但是我看到的所有示例都是在 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)
它不起作用,但它似乎至少有一些意义。我在正确的轨道上吗?
【问题讨论】:
标签: python wpf visual-studio-2010 binding ironpython