Setting DP to UnsetValue should clear bindings
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:
- [ ] Docs have been added/updated which fit documentation template (for bug fixes / features)
- [ ] Unit Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
- [ ] Validated PR
Screenshots Compare Test Runresults. - [ ] Contains NO breaking changes
- [ ] Associated with an issue (GitHub or internal) and uses the automatic close keywords.
- [ ] Commits must be following the Conventional Commits specification.
Other information
Internal Issue (If applicable):
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?
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 This should be ready for review.
Fixed by https://github.com/unoplatform/uno/pull/10782