2017-10-08 6 views
0

에서 속성에 바인딩 나는 다음과 같은 뷰/뷰 모델의 둥지가 있습니다.WPF MVVM 상위 뷰 모델

SomeCustomProperty를 CustomDialogView의 뷰 모델에있는 속성에 바인딩하려고합니다.

가장 좋은 방법은 무엇입니까? 내가 좋아하는 RelativeSource FindAncestor를 통해이 속성의 바인딩을 설정하는 것 같았다 가장 유망한있는 몇 가지, 시도 :

<CustomControl 
    SomeCustomProperty="{ 
     Binding RelativeSource={RelativeSource FindAncestor, 
     AncestorType={x:Type sourcePath:CustomDialogViewModel}}, 
     Path=SomeCustomProperty, 
     Mode=OneWay/> 
</CustomControl> 

을하지만 난 더에서 모두 여기에 결합 받고 없습니다 있어요.

베어링이 있지만 CustomListView가 팩토리로 채워 졌는지 확실하지 않습니다.

답변

2

FindAncestor은 바운드 ViewModel이 아닌보기를 찾고 있습니다. 이 사실로 인해 뷰 유형을 AncestorType으로 설정해야합니다. 이제이 뷰의 ViewModel에 PathDataContext을 추가하여 액세스 할 수 있습니다.

<CustomControl 
    SomeCustomProperty="{ 
     Binding RelativeSource={RelativeSource FindAncestor, 
     AncestorType={x:Type sourcePath:CustomDialogView}}, 
     Path=DataContext.SomeCustomProperty, 
     Mode=OneWay/> 
</CustomControl> 
+0

우수! 고맙습니다 :) – amarsha4