Bug 32896 - Trigger with bound Setter's Value doesn't revert View's visual state when condition not matched anymore
Summary: Trigger with bound Setter's Value doesn't revert View's visual state when con...
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.4
Hardware: PC Windows
: High normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2015-08-09 14:29 UTC by Sergey Komisarchik
Modified: 2017-06-21 21:00 UTC (History)
10 users (show)

Tags: binding, datatrigger
Is this bug a regression?: ---
Last known good build:


Attachments
test case (891.46 KB, application/octet-stream)
2015-08-09 14:33 UTC, Sergey Komisarchik
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description Sergey Komisarchik 2015-08-09 14:29:15 UTC
Setter has Value as bindable property. So it can be bound to some BindingContext with Binding.
But it doesn't revert View's state when condition is not matched anymore - binding still applied.

Steps to reproduce:
1. set trigger setter and apply Binding to Value: <Setter Property="PropertyXXX" Value={Binding ...}"
2. satisfy trigger's condition
3. unsatisfy trigger's condition.
4. Result: Binding still applied.

Tested with 1.4.4, 1.5.0-pre1

Complete minimal example:

public class App : Application
{
    public App()
    {
        this.MainPage = new Page1();
    }
}

public class Page1 : ContentPage
{
    public Page1()
    {
        var entry = new Entry() { Placeholder = "Type '42'" };
        var button = new Button() { Text = "Clear", Command = new Command(delegate() { entry.Text = null; }) };

        button.Triggers.Add(
            new DataTrigger(typeof(Button))
            {
                Binding = new Binding("Text") { Source = entry },
                Value = "42",
                Setters =
                {
                    new Setter()
                    {
                        Property = Button.BackgroundColorProperty,
                        Value = new Binding("Color") { Source = new { Color = Color.Green } },  // bug
                        //  Value = Color.Green                                                 // works
                    }
                }
            });
            
        this.Content = new StackLayout() { VerticalOptions = LayoutOptions.Center, Children = { entry, button }};
    }
}
Comment 1 Sergey Komisarchik 2015-08-09 14:33:50 UTC
Created attachment 12421 [details]
test case
Comment 2 Abhishek 2015-08-24 13:14:53 UTC
I have tried this issue and unable to reproduce the reported behavior at my end.

Steps To Reproduce:
1. Download the attached sample.
2. Uncomment Value = new Binding("Color") { Source = new { Color =
Color.Green } }, 
3. Run the application on Device/Simulator or Emulator. As I am not getting any changes while commenting the above line code and uncomment Value = Color.Green As I have checked it behavior both iOS and Android.

Could you please let me know what changes I will notice while performing the steps 2 and 3. Also provide the IDE log, Device Log, Environment Info

IDE log via Help->Xamarin->Zip Xamarin Logs.
Environment Info via Help->About Microsoft Visual Studio.

Screencast: http://www.screencast.com/t/hKxNeorvX

Environment Info:

Xamarin.Forms: 1.5.0.6396-pre1

Microsoft Visual Studio Professional 2013
Version 12.0.40629.00 Update 5
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Professional

LightSwitch for Visual Studio 2013   06177-004-0446016-02312
Microsoft LightSwitch for Visual Studio 2013

Team Explorer for Visual Studio 2013   06177-004-0446016-02312
Microsoft Team Explorer for Visual Studio 2013

Visual Basic 2013   06177-004-0446016-02312
Microsoft Visual Basic 2013

Visual C# 2013   06177-004-0446016-02312
Microsoft Visual C# 2013

Visual C++ 2013   06177-004-0446016-02312
Microsoft Visual C++ 2013

Visual F# 2013   06177-004-0446016-02312
Microsoft Visual F# 2013

Visual Studio 2013 Code Analysis Spell Checker   06177-004-0446016-02312
Microsoft® Visual Studio® 2013 Code Analysis Spell Checker

Portions of International CorrectSpell™ spelling correction system © 1993 by Lernout & Hauspie Speech Products N.V. All rights reserved.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Electronic version licensed from Lernout & Hauspie Speech Products N.V. All rights reserved.

Windows Phone SDK 8.0 - ENU   06177-004-0446016-02312
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools   12.5.60612.0
Microsoft Web Developer Tools contains the following components:
Support for creating and opening ASP.NET web projects
Browser Link: A communication channel between Visual Studio and browsers
Editor extensions for HTML, CSS, and JavaScript
Page Inspector: Inspection tool for ASP.NET web projects
Scaffolding: A framework for building and running code generators
Server Explorer extensions for Microsoft Azure Web Apps
Web publishing: Extensions for publishing ASP.NET web projects to hosting providers, on-premises servers, or Microsoft Azure

ASP.NET Web Frameworks and Tools 2012.2   4.1.21001.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.30612.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.4
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Microsoft Advertising SDK for Windows Phone   
Microsoft Advertising SDK for Windows Phone
Build 

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   2.8.60723.765
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Office Developer Tools for Visual Studio 2013 ENU   12.0.30626
Microsoft Office Developer Tools for Visual Studio 2013 ENU

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SAPReferenceManager   1.0
Manages References to Shared Projects

SQL Server Data Tools   12.0.41012.0
Microsoft SQL Server Data Tools

Windows Phone 8.1 SDK Integration   1.0
This package integrates the tools for the Windows Phone 8.1 SDK into the menus and controls of Visual Studio.

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin   3.11.890.0 (c2ca9ac)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   5.1.6.4 (20b25b917aff2ec6f625f946e34e818f0b35cbdb)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.Forms Intellisense   1.0
Provides intellisense for Xamarin.Forms in the XML editor.

Xamarin.iOS   8.10.5.0 (d6242b1be8281dd1638be4d67f4adf48a657df9e)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified

Xamarin.TestCloud.Integration   1.0
Early preview of Xamarin Test Cloud integration
Comment 3 Sergey Komisarchik 2015-08-27 18:16:31 UTC
Abhishek, in step 3 after launching the application you need to satisfy trigger's condition:
3.1) Please type '42' in text box. 
3.2) Then unsatisfy Trigger's condition by clearing text box or type something else
Result: previous state not reverted - binding still applied:
http://screencast.com/t/fYfW2LQ6D7aJ



----------------------------------------------------------------------------
Microsoft Visual Studio Enterprise 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00079

Installed Version: Enterprise

Architecture and Modeling Tools   00322-90150-00969-AA249
Microsoft Architecture and Modeling Tools
    
UML® and Unified Modeling Language™ are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries.

LightSwitch for Visual Studio 2015   00322-90150-00969-AA249
Microsoft LightSwitch for Visual Studio 2015

Visual Basic 2015   00322-90150-00969-AA249
Microsoft Visual Basic 2015

Visual C# 2015   00322-90150-00969-AA249
Microsoft Visual C# 2015

Visual C++ 2015   00322-90150-00969-AA249
Microsoft Visual C++ 2015

Visual F# 2015 RC   00322-90150-00969-AA249
Microsoft Visual F# 2015 RC

Windows Phone SDK 8.0 - ENU   00322-90150-00969-AA249
Windows Phone SDK 8.0 - ENU

.NET Reflector Visual Studio Extension   8.5.0.179
Integrates .NET Reflector into Visual Studio to allow you to seamlessly debug into third-party code and assemblies, even if you don't have the source code for them.

Visit www.reflector.net for more information.

Copyright (c) 2009-2012 Red Gate Software Inc.

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2015 (Beta6)   14.0.20723.0
ASP.NET and Web Tools 2015 (Beta6)

ASP.NET Web Frameworks and Tools 2013   5.2.30624.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.5
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

GenerateUnitTest   1.0
Generates unit test code for methods in classes under test.

GitHub.VisualStudio   1.0
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

ILSpy.AddIn   1.0
Integration of the ILSpy Decompiler into Visual Studio.

Indent Guides   14
Indent Guides

Adds visual guides at each indentation level.

JetBrains ReSharper Ultimate 2015.2   Build 103.0.20150818.200216
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2015 JetBrains, Inc.

Microsoft Azure HDInsight HQL Service   2.0.2200.0
Language service for Hive query

Microsoft Azure HDInsight Tools for Visual Studio   2.0.2200.0
An integrated development environment for HDInsight application development.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

Microsoft Azure Tools   2.7
Microsoft Azure Tools for Microsoft Visual Studio 2015 - v2.7.30728.1602

Microsoft Code Digger   0.9
Microsoft Code Digger

Microsoft.Pex.VisualStudio   1.0
Pex

Multilingual App Toolkit   4.0.1393.0
Multilingual App Toolkit helps you localize your Windows Store app by providing file management, pseudo and machine translation, translation editor, and build integration. http://go.microsoft.com/fwlink?linkid=245767

NuGet Package Manager   3.0.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Office Developer Tools for Visual Studio 2015 ENU   14.0.23025
Microsoft Office Developer Tools for Visual Studio 2015 ENU

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   14.0.50730.0
Microsoft SQL Server Data Tools

Visual Studio Tools for Universal Windows Apps   14.0.23121.00 D14OOB
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

VSColorOutput   1.4.5
Color output for build and debug windows - http://blueonionsoftware.com/vscoloroutput.aspx

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin   3.11.893.0 (dd3b114)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   5.1.6.5 (965922617b643927fd4a01adae68730b63a01ecb)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.Forms Intellisense   1.0
Provides intellisense for Xamarin.Forms in the XML editor.

Xamarin.iOS   8.10.5.0 (67572797c1794b8219673503e84709d6a775628a)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified

Xamarin.TestCloud.Integration   1.0
Early preview of Xamarin Test Cloud integration
Comment 4 Kevin B 2016-01-06 20:19:57 UTC
I too have run into this bug. As Sergey has demonstrated in his example the problem does not occur with static values, but it does occur with bindings. 

Doing some digging into the de-compiled source for Xamarin.Forms.Setter.
The Apply method (which gets called when the trigger's condition matches) check the value for the setter and either calls SetBinding, SetDynamicResource, or SetValue depending on if the value is a BindingBase, DynamicResource, or any other value respectively. On the UnApply method (which gets called when the trigger's condition is no longer matched) it will either call SetValue (if the target property's value was not its default when the setter was applied) or ClearValue (for every other case). 

This is where the problem is, the changes that the Apply method makes are not properly reverted by the UnApply method.
Comment 5 Kevin B 2016-01-06 20:21:01 UTC
Also worth noting, I am running Xamarin.Forms v2.0.0.6490
Comment 6 Chris King 2016-04-13 18:24:05 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Forms. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction.
Comment 7 Chris King 2016-04-13 18:27:17 UTC
Actually, decided to re-investigate ourselves.
Comment 8 Philipp Sumi 2017-01-10 09:17:11 UTC
I'm seeing this on Xamarin 2.3.3:

- Custom control with a bindable property (TextColor)
- Trigger upon the IsValid bindable property, which changes TextColor.

So far, so good, but once IsValid is back to true, the control still retains the error. And since I don't know the original TextColor, I can't just add a second trigger to set the "IsValid == true" color.

What's the status on this? Triggers are awesome, but somewhat useless without the ability to revert...

Thanks,
Philipp
Comment 9 Philipp Sumi 2017-01-10 09:30:26 UTC
Just observed: It seems to make a difference whether there is one or multiple bindings:

The example below keeps the "Error" color if the "IsValid" property is reset. However, if I remove the first trigger, behavior works as expected.

                <BoxView.Triggers>
                    <DataTrigger TargetType="BoxView"
                                 Binding="{Binding Source={x:Reference Entry}, Path=IsFocused}"
                                 Value="True">
                        <Setter Property="BackgroundColor"
                                Value="{StaticResource Accent}" />
                    </DataTrigger>
                    <DataTrigger TargetType="BoxView"
                                 Binding="{Binding IsValid}"
                                 Value="False">
                        <Setter Property="BackgroundColor"
                                Value="{StaticResource ErrorColor}" />
                    </DataTrigger>
                </BoxView.Triggers>
Comment 10 Taylor Buchanan 2017-03-07 17:23:04 UTC
I'm seeing this issue in Xamarin.Forms 2.3.4.192-pre2 when I enable XAML compilation:

    [assembly: XamlCompilation(XamlCompilationOptions.Compile)]

When I remove this, the issue no longer occurs.
Comment 11 Stephane Delcroix 2017-06-21 13:56:57 UTC
I believe this bug no longer exists. I added a test for it to make sure it doesn't regress

https://github.com/xamarin/Xamarin.Forms/commit/ded7d4e17b06a30e3e004f49f5bf2f3399a23548

@Philipp:
Your issue is slightly different. You're assigning the same property from multiple DataTriggers, and in that case, the result depends on which order the triggers are applied and unapplied. We probably spend days specifying this without agreeing on a spec that fits everyone need. A MultiBinding (which doesn't exists in XF would probably  works for you, and a MultiTrigger (which definitely exists) will, provided that, like in your original problem, you have no overlap.

You still need to MultiTriggers:
- in the first, set the BackgroundColor to `Accent` if the binding to entry.IsFocused is true AND the binding to vm.IsValid is true.
- in the second, set the BackgroundColor to `Error` if the binding to vm.IsValid is false (you can use a normal DataTrigger here)

This will work as https://bugzilla.xamarin.com/show_bug.cgi?id=30074 is about making sure the no longer valid conditions are unapplied before the new ones are applied.

Your XAML could looks like this (I'm typing this out of my head, in this editor. it could contains errors)

<BoxView.Triggers>
  <MultTrigger TargetType="BoxView">
    <MultiTrigger.Conditions>
      <BindingCondition Binding="{Binding Source={x:Reference Entry}, Path=IsFocused}" Value="True" />
      <BindingCondition Binding="{Binding IsValid}" Value="False" />
    </MultiTrigger.Conditions>
    <Setter Property="BackgroundColor" Value="{StaticResource Accent}" />
  </MultiTrigger>
  <DataTrigger TargetType="BoxView" Binding="{Binding IsValid}" Value="False">
    <Setter Property="BackgroundColor" Value="{StaticResource ErrorColor}" />
  </DataTrigger>
</BoxView.Triggers>

If that doesn't work, please open a new issue, and assign it to me. Thanks.

@Taylor:
This looks very strange, as that shouldn't be related. Could you please open a new issue with a reproduction case attached ? Thanks.
Comment 12 Philipp Sumi 2017-06-21 19:47:54 UTC
Stephane, thanks for taking the time for this detailed answer! I really appreciate it :)
Comment 13 Stephane Delcroix 2017-06-21 20:08:32 UTC
@philipp, let me now if that works for you. I'm kinda curious, but haven't found the time yet to test
Comment 14 Philipp Sumi 2017-06-21 20:15:35 UTC
Your suggestions sounds like a feasible approach with the bug fix you linked to. I'm definitely keeping that one in mind. As far as that use case goes, I couldn't tell you - I simply delegated the whole work with the conditionals to my model instead of using triggers, which works just fine as well. As you mentioned, MultiBindings would be a very welcome addition here though :)
Comment 15 Kevin B 2017-06-21 20:24:17 UTC
@philipp If you are interested there are some ways to get MultiBinding like functionality in XF. http://intellitect.com/multibinding-in-xamarin-forms/
Comment 16 Philipp Sumi 2017-06-21 21:00:10 UTC
@kevin - thanks for the link!