星期日, 8月 06, 2017

[X.Form] Data Trigger

根據這篇官方文章 Triggers 的練習,該範例和筆記
[X.Form] IValueConverter - Entry 必填
是相同範例,只是作法不同而已

Data Trigger 說明
uses data binding to trigger based on the properties of another control.
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:local="clr-namespace:MVVMPractice"
             x:Class="MVVMPractice.XF3_DataTrigger">
 
    <ContentPage.Content>
        <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
            <Entry x:Name="entry" Text="" Placeholder="required field"></Entry>
            <Button Text="Save" FontSize="Large" HorizontalOptions="Center">
                <Button.Triggers>
                    <DataTrigger
                        TargetType="Button"
                        Binding="{Binding Source={x:Reference entry},Path=Text.Length}"
                        Value="0">
                            <Setter Property="IsEnabled" Value="False"></Setter>
                    </DataTrigger>
                </Button.Triggers>
            </Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
執行結果-1

[X.Form] DataTrigger-1

執行結果-2

[X.Form] DataTrigger-2

沒有留言:

張貼留言