2011-03-28 4 views
1

Silverlight 응용 프로그램이 있습니다. 이 응용 프로그램에는 UserControl이 있습니다. 이 UserControl에는 DataContext에 할당 된 ParentViewModel이라고하는 유형의 뷰 모델이 있습니다. 또한이 UserControl에는 두 개의 자식 UserControl 요소가 있습니다. 이러한 각 UserControl 요소에는 ChildViewModel을 DataContext에 할당하는 뷰 모델이 있습니다.Silverlight의 DataContext 상속

사용자가 하위 UserControl 요소 중 하나의 단추를 클릭하면 두 번째 자식 UserControl에 영향을 주길 원합니다. 나는이 일을하고있다. 내 문제는 내 ParentViewModel 각 자식 UserControl 요소에 바인딩 할 속성이 있습니다. 내 ParentViewModel의 속성에 어떻게 바인딩합니까? 나는 항상 Tree를 통해 Propogate 된 DataContext라고 생각했다. 하지만 내가 틀린 것 같습니다.

내가 시도하고있는 것을 할 수 있습니까? 감사!

답변

1

뷰 모델에서 계층 구조를 만듭니다. 각 자식 뷰 모델에는 부모에 대한 참조가 들어 있습니다. F.i. XAML에서

public class ChildViewModel1 :... 
{ 
public ChildViewModel1(ParentViewModel parentViewModel) 
{ 
_parentViewModel = parentViewModel; 
} 
private ParentViewModel _parentViewModel; 
public ParentViewModel ParentViewModel {get {return _parentViewModel; }} 
} 

:

<TextBlock Text={Binding ParentViewModel.NeededProperty}/>