Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does DataContext="{Binding}" mean?

Tags:

I'm trying to find out where the items in a HeaderedContentControl come from in a project that's not mine. Here's the code:

        <HeaderedContentControl              Content="{Binding Path=Workspaces}"             ContentTemplate="{StaticResource WorkspacesTemplate}"             Header="Workspaces"             Style="{StaticResource MainHCCStyle}"              DataContext="{Binding}" // <--- this         />  <DataTemplate x:Key="WorkspacesTemplate"> <TabControl    IsSynchronizedWithCurrentItem="True"    ItemsSource="{Binding}"    ItemTemplate="{StaticResource ClosableTabItemTemplate}"   Margin="4"   /> 

so let's examine it:

  1. ContentTemplate attribute describes how the items are desplayed.
  2. WorkspacesTemplate sets ItemsSource's attribute to {Binding} meaning it's bound to its DataContext property (DataContext of HeaderedContentControl)
  3. So I look at HeaderedContentControl's dataContext, but it is described as "{Binding}" as well...

What does that mean?

like image 632
Ivan Prodanov Avatar asked Dec 20 '12 08:12

Ivan Prodanov


People also ask

What is DataContext?

Data context is the network of connections among data points. Those connections may be created as metadata or simply identified and correlated. Contextual metadata adds value, essentially making it possible to receive information from data. A single data point on its own is useless.

What is the difference between DataContext and ItemsSource?

DataContext does not generate template, it only used to hold common data for other controls to bind. In terms of ItemsSource property, it is mainly used to generate template regardless of you set it in XAML or in the code behind. DataContext is mainly used to hold common data that other child want to share.

What is DataContext in WPF?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

What is binding why binding of data is necessary?

What is data binding? Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.


2 Answers

Without seeing more of your code it is hard to be certain, but DataContext="{Binding}" is quite often unnecessary, as any object in the current binding context will automatically have its DataContext property set to the equivalent of {Binding}.

Remember:

  • Property="{Binding}" means "set this.Property to the evaluated value of this.DataContext"
  • Property="{Binding Path=SubProperty}" means "set this.Property to the evaluated value of this.DataContext.SubProperty"
  • etc

This means that DataContext="{Binding}" means "set this.DataContext to the evaluated value of this.DataContext", which (in most cases) is redundant!

like image 163
Steve Greatrex Avatar answered Oct 14 '22 08:10

Steve Greatrex


{Binding} is something like bind 'this' or current data context - assigned or inherited from parents. For better understanding, equivalent for {Binding} is {Binding .} or {Binding Path=.}

like image 24
Jozef Cechovsky Avatar answered Oct 14 '22 09:10

Jozef Cechovsky