Ric*_*lay 13
它总是可以省略,因为它是Binding XAML扩展的默认属性.仅在使用多个属性时才明确指定.
Phi*_*hil 12
这是因为Binding类有一个默认构造函数,当你有绑定时使用,{Binding FallbackValue='HelloWorld', Path=MyProperty}并且构造函数有一个参数Path.
因此,当存在属性/值对列表时,绑定将创建为
new Binding(){
Path="MyProperty"
ElementName="MyElement"
}
Run Code Online (Sandbox Code Playgroud)
第二种形式用于绑定{Binding MyProperty, ...}.在这种情况下,绑定创建为
new Binding("MyProperty"){
ElementName = "MyElement",
...
}
Run Code Online (Sandbox Code Playgroud)
指定Path =总是正确的(也可能更正确),但没有它就可以逃脱.
Path用于指定要绑定的基础对象的属性名称.
绑定到DataContext时,可以省略Path:
{Binding MyProperty}
{Binding Path=MyProperty}
Run Code Online (Sandbox Code Playgroud)
当您需要指定DataContext以外的源时,您可以使用 Source,, RelativeSource或ElementName引用该对象,因此您通常必须指定要为其设置绑定的属性:
<Button Background="{Binding ElementName=refButton, Path=Background}"/>
<TextBlock Width="{Binding RelativeSource={RelativeSource Self}, Path=Parent.ActualWidth}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17557 次 |
| 最近记录: |