102

What does {Binding Path=.} mean in a WPF binding?

I see some people use it, but couldn't find any explanation.

Are there any other special symbols in binding syntax (other than {Binding /})?

g t
  • 7,287
  • 7
  • 50
  • 85
Sergey Aldoukhov
  • 22,316
  • 18
  • 72
  • 99

2 Answers2

87

I found this WPF Binding CheatSheet a few months back and find it very useful, especially for anyone learning WPF. There are some spelling mistakes within it, but it is still quite good.

Here is a small excerpt (which is supposed to have tabular formatting):

Basic Binding
{Binding} Bind to current DataContext.
{Binding Name} Bind to the “Name” property of the current DataContext.
{Binding Name.Length} Bind to the Length property of the object in the Name property of the current DataContext.
{Binding ElementName=SomeTextBox, Path=Text} Bind to the “Text” property of the element XAML element with name=”SomeTextBox” or x:Name=”SomeTextBox”.

Direct link to CheatSheet

greybeard
  • 2,249
  • 8
  • 30
  • 66
Ryan Versaw
  • 6,417
  • 3
  • 30
  • 31
  • 4
    nice one but what is {Binding .}. I am searching explanation for this one but I cant find it? – Emil Apr 24 '16 at 11:56
  • @batmaci I want to say it's equivalent to `{Binding }`, but it's been quite a few years since I've worked much with Xaml, so I'm not sure. – Ryan Versaw May 04 '16 at 13:42
  • 1
    @batmaci, years later... `{Binding string}` is similar to `{Binding Path=string}`. `string` is not used to initialize the property `Path` directly, but as a parameter for the constructor `Binding (string path)` which then initializes `Path` property. This works only when the string is the first token after `Binding`, the rest being regular initializers (property=value pairs). Knowing that, `Binding .` is in effect equal to `Binding Path=.` and property Path syntax is available [here](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/propertypath-xaml-syntax?view=netframework-4.8). – mins Nov 21 '19 at 18:49
70

This is shorthand for binding to the current source. For more info see here.

From the documentation specifically:

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

Martin
  • 16,093
  • 1
  • 29
  • 48
micahtan
  • 18,530
  • 1
  • 38
  • 33
  • 1
    @Ray - my thoughts exactly. If you're going to shorthand, why stop there? – micahtan Jun 30 '09 at 22:17
  • 5
    I can't argue with MSDN, but there still a difference. {Binding} cannot be used on an object itself (being two way it requires a property) while {Binding Path=.} works. – Sergey Aldoukhov Jun 30 '09 at 22:46
  • @Sergey - Do you have a code sample where {Binding} doesn't work but {Binding Path=.} does? If so, MSDN probably needs to get updated... – micahtan Jun 30 '09 at 23:32
  • Easily: var s = "test; DataContext=s; and in - does not work. But MSDN is not wiki... – Sergey Aldoukhov Jul 01 '09 at 16:55
  • 9
    Another exemple : does not work because you have to set the Path. In that case Path=. is useful. – Nicolas Dec 02 '10 at 17:41
  • {Binding} and {Binding Path=.} are not exactly equal. This [Question](http://stackoverflow.com/questions/5488014/are-binding-path-and-binding-really-equal) points out the difference. – Grochni Aug 28 '14 at 10:43
  • So `Visibility="{Binding IsReadOnly}` is the same as `Visibility="{Binding path=IsReadOnly}` ? – Zoli Apr 03 '23 at 09:15