Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is DataContext for?

As a continuation of the question Linking DataContext with another property in WPF.

At the very end of the research I was very surprised to find out that when one writes something like that:

<Label Content="{Binding Path=Name}" /> 

The DataContext against which the Content property is binded is of the Label control itself! The fact that it still works is due to the default inheritance of the DataContext value from the nearest parent.

But if you have this label wrapped in a custom control, and you don't want to bind your data to the DataContext property of that control, you would more likely love to have:

<Controls:SearchSettings Settings="{Binding Path=Settings}" /> 

And here you are. Now you need to set Settings as the DataContext for the SearchSettings control, for Label inside to bind against, but you can't, because that will trigger re-binding of Settings property.

I can't see the point in mixing binding properties using different sources: DataContext, by ElementName, etc. So why would I ever use DataContext?

like image 696
Eugene Strizhok Avatar asked Aug 31 '11 19:08

Eugene Strizhok


People also ask

What is the use of DataContext in WPF?

WPF will search up the element tree until it encounters a DataContext object if a Source or RelativeSource is not used. Once it finds a non- null DataContext , that object is used for binding. It is useful for binding several properties to the same object.

What is DataContext in MVVM?

DataContext is the head of everything. It makes sure that your View is hooked up with ViewModel. There are 3 ways to hook-up View with ViewModel. Within XAML. Code-Behind.

What is the role of DataContext classes in LINQ?

The DataContext class is a LINQ to SQL class that acts as a conduit between a SQL Server database and the LINQ to SQL entity classes mapped to that database. The DataContext class contains the connection string information and the methods for connecting to a database and manipulating the data in the database.

What is DataContext in UWP?

A typical use of DataContext is to set it directly to a data source object. This data source might be an instance of a class such as a business object.


2 Answers

When you write

<Label name="myLabel" Content="{Binding Path=Name}" /> 

you are binding to myLabel.DataContext.Name, and not myLabel.Name.

The XAML in WPF is just a pretty user interface to display and interact with the actual data, otherwise known as the DataContext. The purpose of other binding sources (RelativeSource, ElementName, etc) is to point to another property that doesn't exist in the current control's DataContext


So suppose you have a Window. Without setting the DataContext, the window still displays but there is no data behind it.

Now suppose to set myWindow.DataContext = new ClassA();. Now the data that the window is displaying is ClassA. If ClassA has a property called Name, I could write a label and bind it to Name (such as your example), and whatever value is stored in ClassA.Name would get displayed.

Now, suppose ClassA has a property of ClassB and both classes have a property called Name. Here is a block of XAML which illustrates the purpose of the DataContext, and an example of how a control would refer to a property not in it's own DataContext

<Window x:Name="myWindow"> <!-- DataContext is set to ClassA -->     <StackPanel> <!-- DataContext is set to ClassA -->          <!-- DataContext is set to ClassA, so will display ClassA.Name -->         <Label Content="{Binding Name}" />           <!-- DataContext is still ClassA, however we are setting it to ClassA.ClassB -->         <StackPanel DataContext="{Binding ClassB}">              <!-- DataContext is set to ClassB, so will display ClassB.Name -->             <Label Content="{Binding Name}" />              <!-- DataContext is still ClassB, but we are binding to the Window's DataContext.Name which is ClassA.Name -->             <Label Content="{Binding ElementName=myWindow, Path=DataContext.Name}" />          </StackPanel>     </StackPanel> </Window> 

As you can see, the DataContext is based on whatever data is behind the UI object.

Update: I see this question so often from new WPF users that I expanded this answer into a post on my blog: What is this “DataContext” you speak of?

like image 83
Rachel Avatar answered Sep 30 '22 19:09

Rachel


From CodeProject by kishore Gaddam:

DataContext is one of the most fundamental concepts in Data Binding. The Binding object needs to get its data from somewhere, and there are a few ways to specify the source of the data like using Source property directly in the Binding, inheriting a DataContext from the nearest element when traversing up in the tree, setting the ElementName and RelativeSource properties in the Binding object.

Detailed example on CodeProject: http://www.codeproject.com/Articles/321899/DataContext-in-WPF

like image 31
Cornel Marian Avatar answered Sep 30 '22 17:09

Cornel Marian