xamarin.formsmultitrigger

Xamarin Forms IsEnabled MultiTrigger on Entry is not working


I am trying to get an entry IsEnabled property to set based on 2 different boolean values using a MultiTrigger in Xamarin.Forms 4.6.0.726. I have also tried in the last stable 4.5 version too.

Unfortunately the Entry IsEnabled property seems to remain at whatever the Setter's value is set to (in the case, true).

I have tried the two types of BindingCondition in the below code sample. The first (uncommented) conditions are bound to the IsVisible properties of two other elements on the page. The StackLayout and the Image will toggle their visibility as expected, however the Entry IsEnabled will not changed.

The second snip of code binds directly to the values in the ViewModel, implenenting INotifyPropertyChanged but has the exact same result where the IsEnabled value does not change.

I have run out of ideas and i'm starting to wonder if this is a bug with Xamarin and MultiTriggers. There doesn't seem to be a huge amount of people using them online, and those that do I have mine set out in what seems to be the most common way in the first set of code.

<StackLayout x:Name="ButtonsStack" IsVisible="{Binding Invoice.Editable}">
   <!-- Content Here -->    
</StackLayout>

<Image x:Name="InvoiceImage" IsVisible="{Binding IsUploadInvoice}" />

<StackLayout Orientation="Horizontal" HorizontalOptions="End">
    <Entry Text="{Binding Invoice.TotalAmount}">
        <Entry.Triggers>
            <MultiTrigger TargetType="Entry">
                <MultiTrigger.Conditions>

                    <BindingCondition Binding="{Binding Source={x:Reference ButtonsStack}, Path=IsVisible}" Value="True"/>
                    <BindingCondition Binding="{Binding Source={x:Reference InvoiceImage}, Path=IsVisible}" Value="True"/>

                    <!--<BindingCondition Binding="{Binding Invoice.Editable}" Value="True"/>
                    <BindingCondition Binding="{Binding IsUploadInvoice}" Value="True"/>-->

                </MultiTrigger.Conditions>
                <Setter Property="IsEnabled" Value="True"/>
            </MultiTrigger>
        <Entry.Triggers>
    <Entry>
</StackLayout>

Solution

  • As zafar said, you need to set Entry IsEnable="False" by default, when all the conditions are true, the setter makes the Entry's IsEnabled property true.

    <StackLayout>
                <Entry IsEnabled="False" Text="{Binding Invoice.TotalAmount}">
                    <Entry.Triggers>
                        <MultiTrigger TargetType="Entry">
                            <MultiTrigger.Conditions>
    
                                <BindingCondition Binding="{Binding Source={x:Reference ButtonsStack}, Path=IsVisible}" Value="True" />
                                <BindingCondition Binding="{Binding Source={x:Reference InvoiceImage}, Path=IsVisible}" Value="True" />
    
                            </MultiTrigger.Conditions>
                            <Setter Property="IsEnabled" Value="True" />
                        </MultiTrigger>
                    </Entry.Triggers>
                </Entry>
            </StackLayout>
    

    About Multi triggers, please take a look:

    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/triggers