Why triggers don't work on invisible controls?

JerajoJerajo Member ✭✭

I don't know why the DataTrigger only works when the control is visible, What I'm doing something wrong?

Trigger on Visibility True

<StackLayout
            x:Name="StackLayoutForm"
            IsVisible="{Binding IsFormVisible}">
            <Label Text="Name" />
            <Entry
                x:Name="EntryName"
                Text="Foo">
                <Entry.Triggers>
                    <DataTrigger
                        Binding="{Binding IsFormVisible}"
                        TargetType="Entry"
                        Value="True">
                        <Setter Property="Text" Value="" />
                    </DataTrigger>
                </Entry.Triggers>
            </Entry>
</StackLayout>

Trigger on Visibility False

<StackLayout
            x:Name="StackLayoutForm"
            IsVisible="{Binding IsFormVisible}">
            <Label Text="Name" />
            <Entry
                x:Name="EntryName"
                Text="Foo">
                <Entry.Triggers>
                    <DataTrigger
                        Binding="{Binding IsFormVisible}"
                        TargetType="Entry"
                        Value="False">
                        <Setter Property="Text" Value="" />
                    </DataTrigger>
                </Entry.Triggers>
            </Entry>
</StackLayout>

Output

IsFormVisible == true
EntryName.Text == ""

IsFormVisible == false
EntryName.Text == "Foo"

I hope someone can help me.

Tagged:

Best Answer

  • JerajoJerajo ✭✭
    Accepted Answer

    I tested it both ways with

    <Entry.Triggers>
        <DataTrigger
               Binding="{Binding IsFormVisible}"
               TargetType="Entry"
               Value="False">
                <Setter Property="Text" Value="" />
             </DataTrigger>
    </Entry.Triggers>
    

    But when I change the property IsFormVisible = true the Entry property gets Text = "Foo" I was trying to reset the Entry.Text property each time the StackLayout gets hidden. But at the end I left the reset behavior when it gets visible.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    This trigger will be fired when IsFormVisible's value is true. When it is set to false the Entry will be returned to the previous value: Foo.
    So you will get the result

    IsFormVisible == false
    EntryName.Text == "Foo"
    
  • JerajoJerajo Member ✭✭
    Accepted Answer

    I tested it both ways with

    <Entry.Triggers>
        <DataTrigger
               Binding="{Binding IsFormVisible}"
               TargetType="Entry"
               Value="False">
                <Setter Property="Text" Value="" />
             </DataTrigger>
    </Entry.Triggers>
    

    But when I change the property IsFormVisible = true the Entry property gets Text = "Foo" I was trying to reset the Entry.Text property each time the StackLayout gets hidden. But at the end I left the reset behavior when it gets visible.

Sign In or Register to comment.