Xamarin Forms Binding Basics

Kind 1: Bind the CatName from the page’s Model set to the BindingContext:
<Label Text=”{Binding CatName}></Label>

Kind 2: Bind the CatName from a bindable property on the page control:

Add a Bindable CatName property to the page or control in the code behind:

public static BindableProperty CatNameProperty =
	BindableProperty.Create (
		nameof( CatName),
		typeof ( string ),
		typeof ( Badge ),
		defaultValue: "",
		defaultBindingMode: BindingMode.OneWay
	);

public string CatName{
	get { return ( string ) GetValue( CatNameProperty ); }
	set { SetValue ( CatNameProperty, value ); }
}

Then reference the CatName property on the control. Notice that the x:Name of the control is ‘CatLoveControl’:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CoApp.Views.Common.CatBlock"
             x:Name="CatLoveControl">
    <ContentView.Content>
      <StackLayout>
         <Label Text="{Binding Source={x:Reference CatLoveControl}, Path=CatName}"/>
      </StackLayout>
    </ContentView.Content>
</ContentView>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s