2011-08-10 97 views
0

背景:
我有XAML類似於下面的僞代碼在Silverlight中,子控件視圖模型如何訪問父數據上下文?

... 
<DataContext><vm:parentVM/></DataContext> 
<Grid x:Name="LayoutRoot"> 
... 
<local:myControl Grid.Column="0" Grid.Row="1" DataContext="{Binding vmChild}" /> 

問題:
當我在視圖模型vmChild的情況下處理一些代碼了,我怎麼訪問屬性並在視圖模型vmParent

+0

是你的vmChild在parentVM裏面的嵌套類嗎? –

+0

沒有,完全獨立 – Noah

+0

我假設你正在使用MVVM,將處理的方法處理程序的代碼。 –

回答

1

方法試試這個:假設列表框的數據上下文是在你的childVM和家長是具有從ParentVM設置它的數據上下文的網格。一些鑄造技巧可以讓你訪問商品。

(((sender as ListBox).Parent as Grid).DataContext as ParentVM).VMProperty = "Cool"; 
相關問題