2013-08-24 2 views
2

SomeClassProp.SubTextProp에 대한 바인딩이 작동하지 않습니다 (소스 속성이 텍스트 상자 내용으로 설정되지 않음). TextProp 않습니다.ElementName이 (중첩 된) 하위 속성에 대해 RelativeSource가 작동하지 않습니다.

XAML :

<Window x:Class="TestWPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" 
     Name="wMain" 
     SizeToContent="WidthAndHeight"> 
    <StackPanel> 
     <TextBox Text="{Binding ElementName=wMain, Path=SomeClassProp.SubTextProp}" Width="120" Height="23" /> 
     <TextBox Text="{Binding ElementName=wMain, Path=TextProp}" Width="120" Height="23" /> 
    </StackPanel> 
</Window> 

및 코드 :

public partial class MainWindow : Window 
{ 
    public SomeClass SomeClassProp { get; set; } 
    public string TextProp { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     SomeClassProp = new SomeClass(); 
    } 
} 

public class SomeClass 
{ 
    public string SubTextProp { get; set; } 
} 

내가 여기 뭔가를 분명 실종?

대상 (텍스트 상자)에서 소스 (클래스 속성)로 작업하려면이 바인딩이 필요합니다.

업데이트 : ElementName=wMain에서 RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}으로 바인딩을 변경할 때 - 모두 바인딩이 작동합니다. 따라서 문제는 ElementName 바인딩 속성에만 해당됩니다.

+1

이것이 작동해야하는 이유는 경로라고 불리는 이유입니다. 출력 창에서 binf = ding 오류를 찾았습니까? 확실히 그 문자열이 전혀 설정되지 않았습니까? –

+0

뷰 모델과 함께 작동합니까? –

+0

출력에 바인딩 오류가 없습니다. 'SubTextProp'은 'null'로 남아있는 반면,'TextProp'은 텍스트 상자에 입력 한 것으로 설정됩니다. – lxa

답변

4

마지막으로 문제를 발견했습니다.

 
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 '' 

문제가 있었다 :

인증 된 정의를 바인딩 diag:PresentationTraceSources.TraceLevel=High를 추가 한 후 (정상 올의 부재, 도중에 매우 유용한 것은 "단계별 디버그), I는 출력에 다음 보았다 MainWindow 초기화 순서대로!

그래서 바인딩이 생성 된 순간에 레벨 0 속성 (SomeClassProp)이 아직 초기화되지 않았기 때문에 바인딩이 완전히 실패했습니다 (어떤 이유로 정상 레벨 빙깅 경고를 표시하지 않음).

길고도 짧은 이야기 - InitializeComponent()MainWindow의 생성자 전에 트릭을 SomeClassProp intitialization를 않았다 이동, 너무 ElementName 작업을 시작 바인딩 : - 그것은 RelativeSource 속성을 사용하여 일을하는 이유 -

public MainWindow() 
{ 
    SomeClassProp = new SomeClass(); 
    InitializeComponent(); 
} 

대답을 질문에 거짓말 이러한 출력 라인 로그 :

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

데이터 컨텍스트 초기화으로은 트리 컨텍스트를 필요로하며 Window (그 때까지 SomeClassProperty이 이미 초기화 됨)을 구성한 후 특정 시점으로 연기됩니다.

관련 문제