{Binding PropertyName}与{Binding Path=PropertyName}之间的差异

时间:2021-08-22 13:24:28

I've seen both styles used in the same project, and I wonder if there's any semantic difference between them, or if any would be recommended over the other and why.

我曾在同一个项目中看到过这两种风格,我想知道它们之间是否存在语义上的差异,或者是否推荐其中任何一种,以及为什么。

5 个解决方案

#1


13  

There is none.

没有。

When not specified, the Path property is assigned the value. In other words, Path is the default property of a binding.

未指定时,将为Path属性赋值。换句话说,Path是绑定的默认属性。

It's like the "Content" property, which is the default property for many controls. For example

它类似于“Content”属性,它是许多控件的默认属性。例如

<Button>Hello</Button> Is the same as <Button><Button.Content><TextBlock Text="Hello"/></Button>

<按钮> Hello与 <按钮> <按钮相同。内容> < TextBlock文本= "你好" / > < /按钮>

Hope that helps.

希望有帮助。

#2


41  

There is a significant difference here which you will run into as soon as you have a complex property path with typed parameters.

这里有一个显著的差异,当您有一个具有类型化参数的复杂属性路径时,您将会遇到这个差异。

Conceptionally 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.

它们在概念上是等价的,因为它们最终都设置了绑定。路径,一个通过参数化绑定构造函数,另一个直接通过属性。在内部发生的事情与绑定是非常不同的。Path不仅仅是一个字符串,在这两种情况下都要传递给属性,它是一个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:

解析XAML时,使用类型转换器将字符串转换为属性所期望的类型。因此,当您使用Path=时,会实例化一个PropertyPathConverter来解析字符串并返回一个PropertyPath。不同之处在于

(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:

例如,如果你在一个类中有多个索引器,例如一个需要一个字符串,一个需要一个int类型,你试图将值赋值给后者,那么强制转换将不起作用:

{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.

PropertyPath缺少ITypeDescriptorContext,因为公共构造函数是通过类型系统调用的。无法从字符串sys:Int32中解析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:

如果您使用Path=,但是类型转换器将被使用,类型将通过上下文解析,因此这将会工作:

{Binding Path=[(sys:Int32)0]}

(Aren't implementation details fun?)

(不是实现细节有趣?)

#3


21  

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.

使用其单参数构造函数创建绑定实例,并将值“Foo”传递给该构造函数参数。单参数构造函数只设置Path属性,这就是为什么两个语法是等价的。

It's very much like the syntax for custom attributes, where you can also pass constructor parameters and/or set property values.

它非常类似于自定义属性的语法,在这里您还可以传递构造函数参数和/或设置属性值。

#4


5  

There is no semantic difference, the first property in the binding will be interpreted as the "Path" property if no property name is supplied.

没有语义差异,如果没有提供属性名,绑定中的第一个属性将被解释为“Path”属性。

It's a matter of coding style.

这是编码风格的问题。

Update

更新

Removed the sentence "It is the default property".

删除“它是默认属性”的句子。

I realize that there is no formal support for "default properties", but the scenario is often referred to as the "default property", and is supported by convention.

我意识到没有对“默认属性”的正式支持,但是这个场景通常被称为“默认属性”,并且受到约定的支持。

Example, from the MSDN documentation for the Path property of the Binding markup extension:

例如,从MSDN文档中获取绑定标记扩展的路径属性:

The Binding markup extension uses Binding.Path as a conceptual "default property", where Path= does not need to appear in the expression.

绑定标记扩展使用绑定。路径作为概念上的“默认属性”,其中Path=不需要出现在表达式中。

I do not think I am wrong and completely misguided to use this terminology as is being suggested. I also understand how it is implemented.

我不认为我是错误的和完全被误导了使用这个术语正如所建议的。我也理解它是如何实现的。

#5


3  

Don't think there's any difference, expect perhaps the second is more explicit.

不要认为有什么不同,期待第二点可能更明确。

#1


13  

There is none.

没有。

When not specified, the Path property is assigned the value. In other words, Path is the default property of a binding.

未指定时,将为Path属性赋值。换句话说,Path是绑定的默认属性。

It's like the "Content" property, which is the default property for many controls. For example

它类似于“Content”属性,它是许多控件的默认属性。例如

<Button>Hello</Button> Is the same as <Button><Button.Content><TextBlock Text="Hello"/></Button>

<按钮> Hello与 <按钮> <按钮相同。内容> < TextBlock文本= "你好" / > < /按钮>

Hope that helps.

希望有帮助。

#2


41  

There is a significant difference here which you will run into as soon as you have a complex property path with typed parameters.

这里有一个显著的差异,当您有一个具有类型化参数的复杂属性路径时,您将会遇到这个差异。

Conceptionally 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.

它们在概念上是等价的,因为它们最终都设置了绑定。路径,一个通过参数化绑定构造函数,另一个直接通过属性。在内部发生的事情与绑定是非常不同的。Path不仅仅是一个字符串,在这两种情况下都要传递给属性,它是一个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:

解析XAML时,使用类型转换器将字符串转换为属性所期望的类型。因此,当您使用Path=时,会实例化一个PropertyPathConverter来解析字符串并返回一个PropertyPath。不同之处在于

(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:

例如,如果你在一个类中有多个索引器,例如一个需要一个字符串,一个需要一个int类型,你试图将值赋值给后者,那么强制转换将不起作用:

{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.

PropertyPath缺少ITypeDescriptorContext,因为公共构造函数是通过类型系统调用的。无法从字符串sys:Int32中解析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:

如果您使用Path=,但是类型转换器将被使用,类型将通过上下文解析,因此这将会工作:

{Binding Path=[(sys:Int32)0]}

(Aren't implementation details fun?)

(不是实现细节有趣?)

#3


21  

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.

使用其单参数构造函数创建绑定实例,并将值“Foo”传递给该构造函数参数。单参数构造函数只设置Path属性,这就是为什么两个语法是等价的。

It's very much like the syntax for custom attributes, where you can also pass constructor parameters and/or set property values.

它非常类似于自定义属性的语法,在这里您还可以传递构造函数参数和/或设置属性值。

#4


5  

There is no semantic difference, the first property in the binding will be interpreted as the "Path" property if no property name is supplied.

没有语义差异,如果没有提供属性名,绑定中的第一个属性将被解释为“Path”属性。

It's a matter of coding style.

这是编码风格的问题。

Update

更新

Removed the sentence "It is the default property".

删除“它是默认属性”的句子。

I realize that there is no formal support for "default properties", but the scenario is often referred to as the "default property", and is supported by convention.

我意识到没有对“默认属性”的正式支持,但是这个场景通常被称为“默认属性”,并且受到约定的支持。

Example, from the MSDN documentation for the Path property of the Binding markup extension:

例如,从MSDN文档中获取绑定标记扩展的路径属性:

The Binding markup extension uses Binding.Path as a conceptual "default property", where Path= does not need to appear in the expression.

绑定标记扩展使用绑定。路径作为概念上的“默认属性”,其中Path=不需要出现在表达式中。

I do not think I am wrong and completely misguided to use this terminology as is being suggested. I also understand how it is implemented.

我不认为我是错误的和完全被误导了使用这个术语正如所建议的。我也理解它是如何实现的。

#5


3  

Don't think there's any difference, expect perhaps the second is more explicit.

不要认为有什么不同,期待第二点可能更明确。