WPF 中Binding的2個屬性Source與ElementName的區別


原文出處:http://www.im47.cn/?p=482

ElementName
The ElementName property is used to reference an object by the name of the object. This is particularly useful in XAML, where you can directly reference other elements defined in XAML.

舉例

<StackPanel Background=”Blue”>
<Button x:Name=”refButton” Background=”Orange”/>
<Button Background=”{Binding ElementName=refButton, Path=Background}”/>
</StackPanel>

Source
The Source property is used to specify an object reference on which the binding Path or XPath will be evaluated. The Source property is usually used when the object on which the Binding is set is known and differs from the DataContext.

舉例:

<CustomClass1 Property1=”{Binding Source={x:Static DateTime.Now}, Path=Day}”/>

<CustomClass1 Property1=”{Binding Source={StaticResource AnotherElement}, Path=ActualWidth}”/>

 

簡單地說,“ElementName”用於綁定到xaml界面設計中添加的其它控件對象,“Source”用於綁到資源中的對象。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM