Is there a way to add binding in Value section of data Trigger

stXamDevstXamDev Member ✭✭
edited April 24 in Xamarin.Forms

Answers

  • TaylorDTaylorD USMember ✭✭✭

    Here is the code from the post formatted properly.

    <DataTrigger TargetType="StackLayout" Binding="{Binding IsRAFlagVisible}" Value="{Binding MyVariable}">
        <Setter Property="BackgroundColor" Value="DarkGray"/>
    </DataTrigger>
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    Is this what you are looking for https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/triggers#data-triggers?
    It specifies how to use binding in data trigger.

  • stXamDevstXamDev Member ✭✭

    @TaylorD Thanks
    @LandLu In the code I need to bind the Value field from the code and not as a static value

  • LandLuLandLu Member, Xamarin Team Xamurai

    We can't use binding for the DataTrigger's Value. But we could use a static property to change the value:

    <Entry x:Name="entry" Text="" Placeholder="required field" />
    
    <Button x:Name="button" Text="Save" FontSize="Large" HorizontalOptions="Center">
        <Button.Triggers>
            <DataTrigger TargetType="Button" 
                        Binding="{Binding Source={x:Reference entry}, Path=Text.Length}" 
                        Value="{x:Static local:PublicConstants.TextLength}">
                <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
            </DataTrigger>
        </Button.Triggers>
    </Button>
    

    Define a static property:

    public class PublicConstants
    {
        public static int TextLength { get => 2; }
    }
    

    Moreover, we could use binding for Setter's Value.

Sign In or Register to comment.