Noah
Noah

Reputation: 15320

In Silverlight, how can a child control view model access the parent data context?

BACKGROUND:
I have xaml similar to the following pseudo code

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

QUESTION:
When I am processing some code in the context of the view model vmChild, how do I access properties and methods in the view model vmParent

Upvotes: 0

Views: 573

Answers (1)

The Internet
The Internet

Reputation: 8103

Try this: Assuming the Listbox's data context is in your childVM and the parent is a Grid which has its data context set from the ParentVM. Some casting trickery can allow you to access the goods.

  (((sender as ListBox).Parent as Grid).DataContext as ParentVM).VMProperty = "Cool";

Upvotes: 1

Related Questions