当我从 XAML 创建 WPF 绑定时,为什么 IronPython 2.7 会引发异常?
我刚刚安装了具有 VS 支持的 IronPython 2.7,并尝试创建一个简单的原型 WPF 应用程序。有些东西坏了,可能是在我的安装中,我不知道如何诊断它。我无法让最简单的绑定起作用;他们失败了,但有一个看起来非常非常错误的例外。
我创建一个 WPF 应用程序项目,并将像这样的 XAML 放入我的 WpfApplication1.xaml
文件中:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WpfApplication1">
<Grid>
<TextBox x:Name="MyTextBox" Text="{Binding RelativeSource={RelativeSource Self}, Mode=OneWay, Path=ActualWidth}"/>
</Grid>
</Window>
当我运行此文件时,我收到此异常:
System.Xaml.XamlObjectWriterException was unhandled by user code
Message=Provide value on 'System.Windows.Data.Binding' threw an exception.
InnerException: System.Windows.Markup.XamlParseException
Message=A 'Binding' cannot be set on the 'Text' property of type 'TextBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
嗯,上次我查看时,Text
是一个依赖属性。事实上,如果我在代码中创建绑定,它就会起作用:
import clr
clr.AddReference('PresentationFramework')
from System.Windows import Application, Window, Controls, Data, PropertyPath
class MyWindow(Window):
def __init__(self):
clr.LoadComponent('WpfApplication1.xaml', self)
t = self.FindName("MyTextBox")
b = Data.Binding()
b.RelativeSource = Data.RelativeSource.Self
b.Mode = Data.BindingMode.OneWay
b.Path=PropertyPath("ActualWidth")
t.SetBinding(Controls.TextBox.TextProperty, b)
此时我感到非常困惑。我很难想象任何可能导致此问题的事情都不会完全搞乱从 XAML 创建 WPF 对象。我是否缺少一些明显的东西?
I've just installed IronPython 2.7 with VS support, and am trying to create a simple prototype WPF application. Something is broken, probably in my installation, and I can't figure out how to diagnose it. I can't get the simplest of bindings to work; they fail with an exception that seems really, really wrong.
I create a WPF application project, and put XAML like this in my WpfApplication1.xaml
file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WpfApplication1">
<Grid>
<TextBox x:Name="MyTextBox" Text="{Binding RelativeSource={RelativeSource Self}, Mode=OneWay, Path=ActualWidth}"/>
</Grid>
</Window>
When I run this, I get this exception:
System.Xaml.XamlObjectWriterException was unhandled by user code
Message=Provide value on 'System.Windows.Data.Binding' threw an exception.
InnerException: System.Windows.Markup.XamlParseException
Message=A 'Binding' cannot be set on the 'Text' property of type 'TextBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Hmm, last time I looked, Text
is a dependency property. And indeed, if I create the binding in code, it works:
import clr
clr.AddReference('PresentationFramework')
from System.Windows import Application, Window, Controls, Data, PropertyPath
class MyWindow(Window):
def __init__(self):
clr.LoadComponent('WpfApplication1.xaml', self)
t = self.FindName("MyTextBox")
b = Data.Binding()
b.RelativeSource = Data.RelativeSource.Self
b.Mode = Data.BindingMode.OneWay
b.Path=PropertyPath("ActualWidth")
t.SetBinding(Controls.TextBox.TextProperty, b)
I'm pretty baffled at this point. It's hard for me to imagine anything that could cause this problem that wouldn't also mess up creating WPF objects from XAML completely. Is there something obvious that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是新的 WPF 支持中的一个错误。它已在当前源代码中修复,因此将在 Beta 1 版本中修复。根本原因是需要使用“架构上下文”来获取我们之前未使用过的完整 WPF 语义。它还移动到新的“wpf”模块,而不是位于 clr 模块中。
This is a bug in the new WPF support. It's fixed in the current sources and so it will be fixed in the Beta 1 release. The underlying cause is that there are "schema contexts" which need to be used to get full WPF semantics that we weren't using before. It also moves to a new "wpf" module instead of being in the clr module.