我正在这里跟随一个教程,该教程展示了如何绑定到依赖属性的基本示例。
<Binding ElementName="This" Path="IPAddress" UpdateSourceTrigger="PropertyChanged">
其中"This"是当前窗口的名称:
<Window x:Class="SOTCBindingValidation.Window1" x:Name="This"
每当我尝试做这样的事情时,都会收到相同的错误信息: 无法找到绑定源 'ElementName=GridControlControl1'。BindingExpression:Path=IPAddress; DataItem=null; 目标元素是'TextBox' (Name='AddressBox');目标属性是'Text'(类型为'String') 我的代码:
<UserControl x:Class="WpfGridtest.GridControl" x:Name="GridControlControl1" ... />
<TextBox x:Name="AddressBox">
<TextBox.Text>
<Binding ElementName="GridControlControl1" Path="IPAddress" UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
代码后台:
partial class GridControl : UserControl
public static readonly DependencyProperty IPAddressProperty = DependencyProperty.Register("IPAddress", typeof(string), typeof(GridControl), new UIPropertyMetadata("1.1.1.1"));
public string IPAddress
{
get { return (string)GetValue(IPAddressProperty); }
set { SetValue(IPAddressProperty, value); }
}
这就像是在 .Net 4.0 中发生了一些改变?