【发布时间】:2023-03-26 08:45:01
【问题描述】:
我正在关注here 的教程。它显示了一个关于如何绑定到依赖属性的基本示例。
<Binding ElementName="This" Path="IPAddress" UpdateSourceTrigger="PropertyChanged">
其中“This”是当前窗口的名称:
<Window x:Class="SOTCBindingValidation.Window1" x:Name="This"
每当我尝试做这样的事情时,我都会收到同样的错误:
找不到与引用“ElementName=GridControlControl1”进行绑定的源。绑定表达式:路径=IP地址;数据项=空;目标元素是“文本框”(名称=“地址框”);目标属性是“文本”(类型“字符串”)
我的代码:
<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 中的某些变化?
【问题讨论】:
-
我只是复制了你所做的同样的事情,我似乎没有得到任何错误。
-
你用的是什么框架?
-
好的,感谢您确认这是可行的。我的设置一定有问题。