2017-09-03 34 views

回答

4

BindingBindingObject是Xamarin Forms框架附帶的數據綁定過程的一部分。

數據綁定是在目標(通常是控件屬性,如SwitchIsToggled)和源(通常是包含業務邏輯以及屬性更改通知的視圖模型)之間建立連接的過程。

enter image description here

可以使用Binding定義目標UI控制之間的連接和它的相應的視圖模型作爲源,這是通過BindingContext屬性提供。您可以通過指定Path屬性(以及其他參數,如Converter,StringFormat等)來執行此操作。

例如,在如在上述的圖像稱爲這種情況下,可以定義一個Binding來建立使用BindingContext作爲源(其又是ViewModelEntry.TextViewModel.MyProperty之間的連接。

BindingContext始終被視爲默認源這樣的綁定,除非你明確地指定一個備用源像另一個控制,靜態資源等

例如:

<Switch IsToggled="{Binding IsEnabled}" /> 

將建立一個綁定連接在Switch之間。 IsToggledBindingContextIsEnabled

同樣,

<Switch IsToggled="{Binding IsEnabled, Source={x:Reference siblingControl}}" /> 

將建立Switch之間的結合連接。 IsToggledsiblingControlIsEnabled。您會注意到,BindingContext不是此綁定連接的一部分。

article是學習Xamarin格式綁定的好起點。另一個很好的資源是官方文件herehere。另外,會建議你閱讀如何define bindings using markup-extensions in XAMLextension methods in C#