Difference between {Binding PropertyName} and {Binding Path=PropertyName}
They mean the same thing. Where they differ is in how the Binding object is instantiated and populated.
{Binding Path=Foo}
creates a Binding instance using its parameterless constructor, and then sets the instance's Path property.
{Binding Foo}
creates a Binding instance using its single-parameter constructor, and passes the value "Foo" to that constructor parameter. The single-parameter constructor just sets the Path property, which is why the two syntaxes are equivalent.
It's very much like the syntax for custom attributes, where you can also pass constructor parameters and/or set property values.
There is a significant difference here which you will run into as soon as you have a complex property path with typed parameters.
Conceptually they are equivalent as they both end up setting the Binding.Path
, one via the parameterized Binding
constructor, the other directly via the property. What happens internally is very different though as the Binding.Path
is not just a string which in both cases would be passed on to the property, it is a PropertyPath
.
When XAML is parsed, type converters are used to turn strings into the types expected by properties. So when you use Path=
a PropertyPathConverter
will be instantiated to parse the string and return a PropertyPath
. Now here is the difference:
Binding(string path)
invokespublic PropertyPath(string, Object[])
PropertyPathConverter
invokesinternal PropertyPath(string, ITypeDescriptorContext)
(In the case of the Binding
constructor the Object[]
will be empty)
How does this matter?
If you for example have multiple indexers in a class e.g. one that expects a string
and one that expects an int
and you try to cast the value to target the latter, the cast will not work:
{Binding [(sys:Int32)0]}
The PropertyPath
is lacking the ITypeDescriptorContext
because the public constructor is invoked so the type System.Int32
cannot be resolved from the string sys:Int32
.
If you use Path=
however the type converter will be used instead and the type will be resolved using the context, so this will work:
{Binding Path=[(sys:Int32)0]}
(Aren't implementation details fun?)
There is none.
When not specified, the Path property is assigned the value. In other words, Path is the default property of a binding.
It's like the "Content" property, which is the default property for many controls. For example
<Button>Hello</Button>
Is the same as <Button><Button.Content><TextBlock Text="Hello"/></Button>
Hope that helps.