Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to a property of the DataContext of another control

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 ?

like image 808
eran otzap Avatar asked Mar 11 '12 03:03

eran otzap


People also ask

Which property is related to data binding?

Path property to specify the value to use for your binding. If you're binding to XML data, you use the Binding. XPath property to specify the value. In some cases, it may be applicable to use the Path property even when your data is XML.

What is the data binding concept and How binding works in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What is binding source in WPF?

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.

What is binding in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.


3 Answers

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).

like image 153
JsAndDotNet Avatar answered Sep 27 '22 02:09

JsAndDotNet


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

like image 40
KodeKreachor Avatar answered Sep 23 '22 02:09

KodeKreachor


use simply

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

this element binding.

like image 33
Kylo Ren Avatar answered Sep 23 '22 02:09

Kylo Ren