星期三, 8月 09, 2017

[X.Form] Multi Trigger - 皆須輸入

根據這篇官方文章 Triggers 的練習,該篇筆記的延伸 - [X.Form] Multi Trigger - 任一輸入

Multi Trigger 說明
A MultiTrigger looks similar to a Trigger or DataTrigger except there can be more than one condition. All the conditions must be true before the Setters are triggered.
MultiTriggerConverter Class
// IValueConverter 需引用該 namespace
using Xamarin.Forms;

namespace MVVMPractice
{
    public class MultiTriggerConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value > 0)
            {
                return true; 
            }
            else
            {
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
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_MultiTrigger2">

    <ContentPage.Resources>
        <ResourceDictionary>
            <local:MultiTriggerConverter x:Key="dataHasBeenEntered"/>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
            <Entry x:Name="txtUser" Text="" Placeholder="使用者名稱"/>
            <Entry x:Name="txtPWD" Text="" Placeholder="密碼" IsPassword="True"/>
            <Button x:Name="btnLogin" Text="登錄" IsEnabled="False">
                <Button.Triggers>
                    <MultiTrigger TargetType="Button">
                        <MultiTrigger.Conditions>
                            <BindingCondition Binding="{Binding Source={x:Reference txtUser},
                                                                   Path=Text.Length,
                                                                   Converter={StaticResource dataHasBeenEntered}}"
                                                                   Value="true" />
                            <BindingCondition Binding="{Binding Source={x:Reference txtPWD},
                                                                   Path=Text.Length,
                                                                   Converter={StaticResource dataHasBeenEntered}}"
                                                                   Value="true" />
                        </MultiTrigger.Conditions>
                        <Setter Property="IsEnabled" Value="True" />
                    </MultiTrigger>
                </Button.Triggers>
            </Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
執行結果-1

[X.Form] Multi Trigger - 皆須輸入-1

執行結果-2

[X.Form] Multi Trigger - 皆須輸入-3

執行結果-3

[X.Form] Multi Trigger - 皆須輸入-2

沒有留言:

張貼留言