Search This Blog

Sunday 24 November 2019

Xamarin Forms Trigger (Data Trigger, Property Trigger)

Property Trigger 

Property triggers occur when a property on control is set to a particular value.



Page1.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TestAppDemo.Page1">
    <ContentPage.Content>
        <StackLayout Margin="10">
            <Label Text="Property Trigger" FontSize="Large" />
            <Entry Placeholder="Enter Text">
                <Entry.Triggers>
                    <Trigger TargetType="Entry" Property="IsFocused" Value="True">
                        <Setter Property="BackgroundColor" Value="LightBlue" />
                    </Trigger>
                </Entry.Triggers>
            </Entry>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>





Data Trigger 

Data triggers occur when a property of other control is set. And also you can specify the binding from ViewModel.






Page1.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TestAppDemo.Page1">
    <ContentPage.Content>
        <StackLayout Margin="10">
            <Label Text="Data Trigger" FontSize="Large" />
            <StackLayout Orientation="Horizontal">
                <CheckBox x:Name="chkBox"   />
                <Label Text="IsCheckbox checked" VerticalTextAlignment="Center"  >
                    <Label.Triggers>
                        <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference chkBox},Path=IsChecked }" Value="True" >
                            <Setter Property="Text" Value="Yes!" />
                        </DataTrigger>
                        <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference chkBox},Path=IsChecked }" Value="False" >
                            <Setter Property="Text" Value="No!" />
                        </DataTrigger>
                    </Label.Triggers>
                </Label>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
 

Top 60 Xamarin Blog 


No comments:

Post a Comment

Popular Posts