11

iv'e got 2 panels in an app game

they are both bound to different elements .

 GameDetailsPanel.DataContext = game ; GameBoardPanel.DataContext = gameBoard ; 

*Game has a Turn Property * .

 public Class Game { public bool Turn{ get; set;} } 

now i need to bind one of GameBoardPanel to the value of the Property Turn ,

*for example : something along the lines of *

 <Button Fill={Binding Source=GameDetailsPanel.DataContext , Path=Turn } ></Button> 

how can i reference GameDetailsPanel.DataContext in my binding ?

3 Answers 3

22

For the benefit of searchers, you can bind to the datacontext of another control as detailed here.

The quick solution in my case looked like:

<TextBlock Text="{Binding ElementName=ucClientDetails, Path=DataContext.FullName}"></TextBlock> 

where 'ucClientDetails' was a user control bound to a viewmodel containing client details (including FullName).

Sign up to request clarification or add additional context in comments.

Comments

5

I would recommend having your game and gameBoard properties on a wrapper view model and then setting the datacontext of your view to the wrapper view model. That way in your button's Fill binding you could simply reference the appropriate property on your view model:

public Class Wrapper_ViewModel { public Game game{ get; set; } public T gameBoard{ get; set; } } <Button Fill={Binding Path=game.Turn} ></Button> 

However, for more one-off scenarios you could use the relative source binding attribute and hook another elements data context, as in this example: Access parent DataContext from DataTemplate

2 Comments

yeah i thought of that i just thought maybe i could work around this because i only need it for this specific place.
Check out my edit above for another option if you're looking for a simple one-off binding.
3

use simply

<Button Fill="{Binding ElementName=GameDetailsPanel,Path=DataContext.Turn}"></Button> 

this element binding.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.