Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RelativeSource处理(嵌套)子属性,而ElementName不处理_C#_Wpf_Xaml_Data Binding_Elementname - Fatal编程技术网

C# RelativeSource处理(嵌套)子属性,而ElementName不处理

C# RelativeSource处理(嵌套)子属性,而ElementName不处理,c#,wpf,xaml,data-binding,elementname,C#,Wpf,Xaml,Data Binding,Elementname,下面代码的问题是:绑定到SomeClassProp.SubTextProp不起作用(源属性未设置为textbox内容),而绑定到TextProp则起作用 XAML: 以及守则: 公共部分类主窗口:窗口 { 公共SomeClass SomeClassProp{get;set;} 公共字符串TextProp{get;set;} 公共主窗口() { 初始化组件(); SomeClassProp=新的SomeClass(); } } 公共类 { 公共字符串SubTextProp{get;set;}

下面代码的问题是:绑定到
SomeClassProp.SubTextProp
不起作用(源属性未设置为textbox内容),而绑定到
TextProp
则起作用

XAML:


以及守则:

公共部分类主窗口:窗口
{
公共SomeClass SomeClassProp{get;set;}
公共字符串TextProp{get;set;}
公共主窗口()
{
初始化组件();
SomeClassProp=新的SomeClass();
}
}
公共类
{
公共字符串SubTextProp{get;set;}
}
我是不是漏掉了什么明显的东西

注意,我需要这个绑定来从目标(文本框)到源(类属性)


更新:当我将绑定从
ElementName=wMain
更改为
RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}
时,两个绑定都可以工作。因此,问题是特定于
ElementName
绑定属性的。

好的,最后,我发现了问题

在将diag:presentationontracesources.TraceLevel=High添加到绑定defs(顺便说一句,在没有正常的ol'逐步调试的情况下,非常有用)之后,我在输出中看到了以下内容:

System.Windows.Data Warning: 108 : BindingExpression (hash=54116930): At level 0 - for MainWindow.SomeClassProp found accessor RuntimePropertyInfo(SomeClassProp) System.Windows.Data Warning: 104 : BindingExpression (hash=54116930): Replace item at level 0 with MainWindow (hash=47283970), using accessor RuntimePropertyInfo(SomeClassProp) System.Windows.Data Warning: 101 : BindingExpression (hash=54116930): GetValue at level 0 from MainWindow (hash=47283970) using RuntimePropertyInfo(SomeClassProp): System.Windows.Data Warning: 106 : BindingExpression (hash=54116930): Item at level 1 is null - no accessor System.Windows.Data Warning: 80 : BindingExpression (hash=54116930): TransferValue - got raw value {DependencyProperty.UnsetValue} System.Windows.Data Warning: 88 : BindingExpression (hash=54116930): TransferValue - using fallback/default value '' System.Windows.Data Warning: 89 : BindingExpression (hash=54116930): TransferValue - using final value '' 问题的答案-为什么它使用
RelativeSource
属性工作-在输出日志的以下几行中:

System.Windows.Data Warning: 66 : BindingExpression (hash=28713467): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=28713467): Resolve source deferred

使用
RelativeSource
初始化数据上下文需要树上下文,并且延迟到构建
窗口后的某个时间点(此时
SomeClassProperty
已经初始化)。

这应该可以工作,这就是为什么称为Path。您是否在输出窗口中查找binf=ding错误?是否确实设置了字符串?它是否与视图模型一起工作?输出中没有绑定错误。
SubTextProp
保持为“null”,而
TextProp
设置为我在文本框中键入的任何内容。@retailcoder-不确定这是什么意思;我在某个类上实现了INotifyPropertyChanged,它没有帮助-但这并不重要,因为我正在尝试从目标绑定到源,而不是相反。我的意思是,使用窗口的
DataContext
绑定比使用自己的xaml更容易,您只需要路径部分。如果创建了一个类来保存
TextProp
SomeClassProp
,并将该类的实例分配给窗口的
DataContext
,则可以执行{Binding SomeClassProp.SubTextProp}。
System.Windows.Data Warning: 66 : BindingExpression (hash=28713467): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=28713467): Resolve source deferred