What is View-to-View Binding?
What is View-to-View Binding?
View-to-View Binding in Xamarin.Forms is a technique that allows data to be shared directly between two views on the same page without involving a ViewModel. This is particularly useful when you need to synchronize properties between views, such as updating a label's text based on a slider's value.
XAML Syntax: View-to-View Binding is typically implemented using the x:Reference
markup extension in XAML. This extension allows one view to reference another view directly.
BindingContext: The BindingContext
of the target view is set to the source view using the x:Reference
markup extension. This enables the target view to bind to properties of the source view.
Example:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ReferenceSampleInXamarinForms.MainPage">
<StackLayout>
<Slider x:Name="slider1" Maximum="360" VerticalOptions="CenterAndExpand" />
<Label x:Name="label1" BindingContext="{x:Reference slider1}" Text="{Binding Path=Value}" FontAttributes="Bold" FontSize="Large" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<Label x:Name="label2" BindingContext="{x:Reference label1}" Text="{Binding Path=Text}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
In this example:
Slider
named slider1
is the source view.Label
named label1
binds its Text
property to the Value
property of slider1
.Label
named label2
binds its Text
property to the Text
property of label1
.middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào