WPF中非常基本的依赖性问题—绑定到复杂对象
我刚刚开始学习WPF。 我在xmal中声明了如下文本框:WPF中非常基本的依赖性问题—绑定到复杂对象,wpf,dependency-properties,Wpf,Dependency Properties,我刚刚开始学习WPF。 我在xmal中声明了如下文本框: <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=TestComplex.Something, Mode=TwoWay}"/> TestComplexObject类: public class TestComplexObject : DependencyObject { public string Something { ge
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=TestComplex.Something, Mode=TwoWay}"/>
TestComplexObject类:
public class TestComplexObject : DependencyObject
{
public string Something
{
get { return (string)GetValue(SomethingProperty ); }
set { SetValue(ExeLocationProperty, value); }
}
public static readonly DependencyProperty SomethingProperty =
DependencyProperty.Register("Something", typeof(string), typeof(TestComplexObject), new UIPropertyMetadata("Test Text"));
}
正如您所看到的,我正在尝试将文本框绑定到TestComplex
“'对上的构造函数的调用”
键入“EmuRunner.MainWindow”以
匹配指定的绑定
约束引发了一个异常
编号“6”和行位置“9”
我真的不知道我做错了什么,有人能帮忙吗
提前感谢。问题在于这行:
set { SetValue(ExeLocationProperty, value); }
只允许调用
SetValue(SomethingProperty,value)我相信你会找到你问题的答案
基本上,您的TestComplexObject
不是自由线程和线程安全的(例如,派生自System.Windows.Freezable)。两种类型(MainWindow
和TestComplexObject
)的类型初始值设定项同时(或可能)在不同线程上运行-这将导致交叉线程异常(或更糟)当main窗口的类型初始值设定项
导致调用TestComplexObject
的类型初始值设定项时。框架检测到这种可能性并抛出异常。这不是真的。依赖项属性的名称与它们公开为的CLR属性的名称之间没有必需的关系。CLR属性Foo
映射到依赖属性FooProperty
完全是一种约定。事实上,您可以有多个CLR属性,它们都设置了相同的DP。这是一个非常糟糕的想法,但它不会引发异常。非常感谢,我回家后会检查,但这似乎是问题所在。您是否查看了异常的InnerException属性?
set { SetValue(ExeLocationProperty, value); }