uno icon indicating copy to clipboard operation
uno copied to clipboard

Setting DP to UnsetValue should clear bindings

Open Youssef1313 opened this issue 3 years ago • 4 comments

GitHub Issue (If applicable): closes #9640

PR Type

What kind of change does this PR introduce?

  • Bugfix

What is the current behavior?

Calling ClearValue or SetValue with DependencyProperty.UnsetValue doesn't clear bindings.

What is the new behavior?

Binding is cleared, as in UWP

PR Checklist

Please check if your PR fulfills the following requirements:

Other information

Internal Issue (If applicable):

Youssef1313 avatar Aug 28 '22 08:08 Youssef1313

gitpod-io[bot] avatar Aug 28 '22 08:08 gitpod-io[bot]

The PersonPicture failure (VerifyVSMStatesForPhotosAndInitials) is caused by another inconsistency between Uno and UWP.

I tested the following on UWP:

<StackPanel>
    <local:MyPersonPicture x:Name="personPicture"/>
    <Button x:Name="MyButton" Content="Click here."></Button>
</StackPanel>
    public class MyPersonPicture : PersonPicture
    {
        public TextBlock InitialsTextBlock;

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            InitialsTextBlock = GetTemplateChild("InitialsTextBlock") as TextBlock;
        }
    }

    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();
            this.MyButton.Click += MyButton_Click;
        }

        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            var exp = personPicture.InitialsTextBlock.GetBindingExpression(TextBlock.FontFamilyProperty);
            // !!! NOTE:::  exp is null in UWP
        }
    }

However, in Uno, calling initialsTextBlock.GetBindingExpression(TextBlock.FontFamilyProperty) in the unit test yields a non-null binding. I'm not sure, but it looks like this is a difference in how template binding works in UWP vs Uno?

https://github.com/unoplatform/uno/blob/ac0c34e73910ab04499ff29d724f20d9be475fad/src/Uno.UI/UI/Xaml/Controls/PersonPicture/PersonPicture.xaml#L86

What happens is when VisualStateManager.GoToState is called, we clear the setters here:

https://github.com/unoplatform/uno/blob/ac0c34e73910ab04499ff29d724f20d9be475fad/src/Uno.UI/UI/Xaml/VisualStateGroup.cs#L263

which ends with messing up the template binding since we have this setter:

https://github.com/unoplatform/uno/blob/ac0c34e73910ab04499ff29d724f20d9be475fad/src/Uno.UI/UI/Xaml/Controls/PersonPicture/PersonPicture.xaml#L40

I think fixing up the way template binding works is going to be too complex. @jeromelaban Any workarounds you can think of?

Youssef1313 avatar Aug 30 '22 07:08 Youssef1313

I think fixing up the way template binding works is going to be too complex. @jeromelaban Any workarounds you can think of?

GetBindingExpression is returning StaticResource expressions where it should not, even it is expected to have expressions there (it's used to refresh resources for multiple scenarios). We could probably filter those out, as if I recall properly, they are marked as such.

jeromelaban avatar Aug 30 '22 12:08 jeromelaban

@jeromelaban This should be ready for review.

Youssef1313 avatar Sep 09 '22 13:09 Youssef1313

Fixed by https://github.com/unoplatform/uno/pull/10782

jeromelaban avatar Dec 22 '22 15:12 jeromelaban