Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between DataContext and BindingContext

Tags:

c#

mvvm

wpf

I have been studying databinding techniques in numerous different WPF/MVVM resources. I thought I had a fairly strong grasp on the purpose and use of the DataContext object. But then I came across this article on MVVM (this is a .pdf which was referenced here). The entire chapter doesn't mention DataContext once, but instead utilizes "BindingContext" objects on various content controls. I have never heard of a BindingContext before; is it unique to Xamarin forms or something? Is it an alternate but equivalent way to set up the DataContext for a control?

UPDATE:

While my question in this regard has been answered below, it turns out that I'm not the only one confused by differences such as these. Apparently, the history behind the development of Xamarin.Forms has led to numerous discrepancies with Windows XAML. I am glad to learn that a formal effort to standardize these two paths has just been announced and will include the DataContext terminology.

like image 988
kmote Avatar asked May 19 '17 23:05

kmote


People also ask

What is a DataContext?

The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.

What is BindingContext in xamarin forms?

The BindingContext property indicates the binding source, which is the Slider . The SetBinding method is called on the binding target but specifies both the target property and the source property. The target property is specified as a BindableProperty object: Label.

What is the use of DataContext in WPF?

Every FrameworkElement can be associated with a DataContext which will be used as the default data source during binding, if no other data source is specified in the binding code. Also, the children of this FrameworkElement auotmatically inherit this setting.


1 Answers

Yes; a BindingContext in Xamarin is equivalent to a DataContext in WPF.

There are also BindingContext's in WinForms and ASP.NET but those work differently. You may want to see this blog post for more: http://blog.galasoft.ch/posts/2014/07/using-xamarin-forms-with-mvvmlight/

like image 190
BradleyDotNET Avatar answered Oct 09 '22 18:10

BradleyDotNET