2010-11-20 3 views
1

다음과 같은 데이터 템플릿이있는 경우WPF : DataTemplate에 상대 소스

<DataTemplate DataType="{x:Type vm:EditorTabViewModel}"> 
    <me:MarkdownEditor 
     TextContent="{Binding 
          Path=Content, 
          RelativeSource={RelativeSource AncestorType={x:Type vm:EditorTabViewModel}}}" ... /> 
</DataTemplate> 

왜 상대 소스가 EditorTabViewModel을 찾을 수 없는지 궁금하십니까? 그냥 데이터 템플릿을 말하는가?

업데이트 1

다음은 작동합니다.

<me:MarkdownEditor TextContent="{Binding Path=Content.Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> 

Binding Path=Content.Content 
        ^Refers to EditorTabViewModel.Content 
      ^refers to EditorTabViewModel 

보다 직관적 인 마크 업이 있습니까? 롤, 나는 RelativeSource={RelativeSource Mode=TemplatedParent}}" 이미 EditorTabViewModel

처음 언급 한 줄 알았는데

답변

1

EditorTabViewModel을 의미, 그 DataContext 이미 EditorTabViewModel 될 것입니다. 당신은 모든 소스 설정할 필요가 없습니다 그래서 내가`MarkdownEditor`이있는 UserControl을 경우

TextContent="{Binding Content}" ... /> 
+0

흠을하지만'의 DataContext = this' 나는 그것을 할 필요가 그렇지 않으면이 결합하려고합니다 'MarkdownEditor'의 속성이 맞습니까? –

+0

모든 DataTemplate 내부에서 DataContext는 항상 템플릿이 적용되는 데이터 개체입니다. 귀하의 예제에서 명시 적으로 DataType을 지정하면 DataContext는 해당 유형의 인스턴스가됩니다. –

관련 문제