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

2024-09-19 20:52:08 发布

您现在位置:Python中文网/ 问答频道 /正文

关于如何让数据绑定与复选框一起工作,我见过许多类似的问题,但我看到的所有示例都是用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时自动更新:

^{pr2}$

(该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)

这不管用,但看起来至少有点道理。我走对了吗?在


Tags: namecomhttpuiwindowswindowsystemschemas