gpt4 book ai didi

python - IronPython 和 WPF : Binding a checkbox's IsChecked property to a class member variable

转载 作者:行者123 更新时间:2023-11-28 16:53:39 25 4
gpt4 key购买 nike

我见过许多关于如何使用复选框进行数据绑定(bind)的类似问题,但我见过的所有示例都是用 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 绑定(bind)文档(也是全部在 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)

这行不通,但看起来至少有一定的道理。我走在正确的轨道上吗?

最佳答案

该属性应该使用 INotifyPropertyChanged 接口(interface)。看我的blog有关如何在 IronPython 中实现它的示例。

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

关于python - IronPython 和 WPF : Binding a checkbox's IsChecked property to a class member variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3856905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com