只是学习WPF数据绑定,并在我的理解有差距。我在StackOverflow上看到过一些类似的问题,但我仍然在努力确定我做错了什么。WPF DataBinding在代码隐藏中的标准CLR属性
我有一个简单的人类与名字和姓氏属性(标准CLR属性)。我的Window类中也有一个标准的CLR属性,用于公开一个Person实例。
我已经有了一些XAML,有两种绑定方法。第一部作品,第二部没有。 任何人都可以帮助我理解第二种方法失败的原因吗?输出日志中没有绑定错误消息。
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=MyPerson}">
<StackPanel>
<Label>My Person</Label>
<WrapPanel>
<Label>First Name:</Label>
<Label Content="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=MyPerson.FirstName}"></Label>
</WrapPanel>
<WrapPanel>
<Label>Last Name:</Label>
<Label Content="{Binding MyPerson.Surname}"></Label>
</WrapPanel>
</StackPanel>
编辑:好,非常感谢这么远。我已经改变了第二表达式:
<Label Content="{Binding Surname}"></Label>
我仍然无法得到它的工作虽然!
尝试添加此命名空间的xmlns:诊断=” CLR的命名空间:System.Diagnostics程序;装配= WindowsBase” 并添加绑定诊断:PresentationTraceSources.TraceLevel =高看绑定错误是什么。你的绑定应该看起来像Content =“{Binding Surname,diag:PresentationTraceSources.TraceLevel = High}” – 2010-05-28 12:23:43
每当你有一个数据绑定错误 - 不,每当你调试任何使用数据绑定的WPF应用程序时,有VS的输出窗口打开并在屏幕上可见,这样你就可以看到绑定写入它的许多错误消息,当它不工作时。 – 2010-05-28 18:53:57
此问题的标题应该正确,以读取“WPF DataBinding to * XAML *中的标准CLR属性”。在代码隐藏中绑定是一个完全不同的问题。 – kpozin 2010-10-06 21:11:29