0

Have read few articles on WPF and I notice that Binding is often used in different ways:

  • {Binding UserName} binds to the UserName property of the UserViewModel class.
  • {Binding Path=UserName} same as above.
  • {Binding}. Notice, it is used by itself, there is no Path or property name provided. I have seen this but don't understand how does this bind to anything?

For example, I have seen this in the article which has a DataTemplate defining this:

<ContentControl Margin="20,10" Content="{Binding Path=CurrentProduct}" />

, then in MainWindow.xaml, the Binding is used like this

<ContentControl Content="{Binding}" HorizontalAlignment="Center" Margin="10" />

Why it is only Binding by itself used here and what does it mean?

pixel
  • 9,653
  • 16
  • 82
  • 149

3 Answers3

1

The Binding class has many properties, one of which is the Path property.

  • <ContentControl Content="{Binding Path=CurrentProduct}" /> -- The XAML engine will call the default (parameterless constructor) and assign the property Path with "CurrentProduct".

  • <ContentControl Content="{Binding CurrentProduct}" /> -- The XAML engine will parse the XAML provided that there is a constructor that can take that value, which the Binding class does have!

  • <ContentControl Content="{Binding}" /> -- The XAML engine will call the default (parameterless constructor), and since no value was assigned to the property path, it will use the value from DataContext.

myermian
  • 31,823
  • 24
  • 123
  • 215
1
  1. The following statements are equivalent:

    • {Binding UserName} binds to UserName property of UserViewModel
    • {Binding Path=UserName} same as above

    because of the following points.

    • Such usage: {Binding …}Binding markup extension usage. Please note the inheritance hierarchy of the Binding class: here is the MarkupExtension class.
    • The second statement uses implicit path:

      Implicit Path

      The Binding markup extension uses Binding.Path as a conceptual "default property", where Path= does not need to appear in the expression. If you specify a Binding expression with an implicit path, the implicit path must appear first in the expression, prior to any other bindProp=value pairs where the Binding property is specified by name. For example: {Binding PathString}, where PathString is a string that is evaluated to be the value of Binding.Path in the Binding created by the markup extension usage. You can append an implicit path with other named properties after the comma separator, for example, {Binding LastName, Mode=TwoWay}.

      Binding Markup Extension, MSDN.

    So, it is a matter of choice which of them to use.

  2. The following statement:

    • {Binding}

    represents a binding to the current source:

    • Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}".

    <…>

    As long as the binding already has a data context (for instance, the inherited data context coming from a parent element), and whatever item or collection being returned by that context is appropriate for binding without requiring further path modification, a binding declaration can have no clauses at all: {Binding}. This is often the way a binding is specified for data styling, where the binding acts upon a collection. For more information, see the "Entire Objects Used as a Binding Source" section in the Binding Sources Overview.

    Windows Presentation Foundation: Data Data Binding (WPF): Binding Declarations Overview, MSDN.

0

What you ask is simply a XAML extension in WPF and it's usually named as WPF Data Binding.

Data Binding in WPF is quite complex, but you should already read in the MSDN Library documentation of WPF first, since it's quite detail and it's of course the official and the most definitive resource.

I suggest you visiting MSDN Library for WPF data binding first. You should start from this page: Data Binding (WPF).

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49