Skip to content

[any-plat] DependencyProperty changed callback not firing when set via style setter #3398

Closed

Description

@Xiaoy312

Current behavior

DependencyProperty Changed event not proc'ing when the dp is set from style setter.

Expected behavior

Should work just like uwp.

How to reproduce it (as minimally and precisely as possible)

DpChangedTest.zip

Environment

Nuget Package: Uno.UI

Package Version(s): 3.0.0-dev.593
Affected platform(s):

  • iOS
  • Android
  • WebAssembly
  • WebAssembly renderers for Xamarin.Forms (not sure)
  • macOS (not sure)
  • Windows
  • Build tasks
  • Solution Templates

Visual Studio:

  • 2017 (version: )
  • 2019 (version: )
  • for Mac (version: )

Relevant plugins:

  • Resharper (version: )

Anything else we need to know?

This issue was not present in 2.4.0.

Activity

  1. added
    kind/bug
    platform/wasm 🌐
    platform/android 🤖
    platform/ios 🍎
    triage/untriaged
    on Jun 18, 2020
  2. Xiaoy312 commented on Jun 18, 2020

    @Xiaoy312
    ContributorAuthor

    TestControl:

    public partial class TestControl : ContentControl
    {
    	#region Property: Header
    
    	public static DependencyProperty HeaderProperty { get; } = DependencyProperty.Register(
    		nameof(Header),
    		typeof(string),
    		typeof(TestControl),
    		new PropertyMetadata(default));
    	public string Header
    	{
    		get => (string)GetValue(HeaderProperty);
    		set => SetValue(HeaderProperty, value);
    	}
    
    	#endregion
    	#region Property: Property1
    
    	public static DependencyProperty Property1Property { get; } = DependencyProperty.Register(
    		nameof(Property1),
    		typeof(string),
    		typeof(TestControl),
    		new PropertyMetadata(default, OnProperty1Changed));
    	public string Property1
    	{
    		get => (string)GetValue(Property1Property);
    		set => SetValue(Property1Property, value);
    	}
    
    	#endregion
    	#region Property: Property2
    
    	public static DependencyProperty Property2Property { get; } = DependencyProperty.Register(
    		nameof(Property2),
    		typeof(string),
    		typeof(TestControl),
    		new PropertyMetadata(default, OnProperty2Changed));
    	public string Property2
    	{
    		get => (string)GetValue(Property2Property);
    		set => SetValue(Property2Property, value);
    	}
    
    	#endregion
    
    	private static void OnProperty1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    	{
    		(sender as TestControl)?.AddLine($"OnProperty1Changed: {e.NewValue}");
    	}
    
    	private static void OnProperty2Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    	{
    		(sender as TestControl)?.AddLine($"OnProperty2Changed: {e.NewValue}");
    	}
    
    	private void AddLine(string text)
    	{
    		Content = string.IsNullOrEmpty(Content?.ToString())
    			? text
    			: Content + "\n" + text;
    	}
    }

    Xaml:

    <Page.Resources>
    	<Style x:Key="TestControlStyle" TargetType="local:TestControl">
    		<Setter Property="Template">
    			<Setter.Value>
    				<ControlTemplate TargetType="local:TestControl">
    					<StackPanel BorderBrush="Black" BorderThickness="1">
    						<TextBlock Text="{TemplateBinding Header}" />
    						<Rectangle Height="1" Fill="Black" HorizontalAlignment="Stretch" />
    						<TextBlock Text="{TemplateBinding Property1}" />
    						<TextBlock Text="{TemplateBinding Property2}" />
    						<Rectangle Height="1" Fill="Black" HorizontalAlignment="Stretch" />
    						<TextBlock Text="{TemplateBinding Content}" />
    					</StackPanel>
    				</ControlTemplate>
    			</Setter.Value>
    		</Setter>
    	</Style>
    	<Style x:Key="TestControlStyleWithSetter" TargetType="local:TestControl">
    		<Setter Property="Property2" Value="Asd" />
    		<Setter Property="Template">
    			<Setter.Value>
    				<ControlTemplate TargetType="local:TestControl">
    					<StackPanel BorderBrush="Black" BorderThickness="1">
    						<TextBlock Text="{TemplateBinding Header}" />
    						<Rectangle Height="1" Fill="Black" HorizontalAlignment="Stretch" />
    						<TextBlock Text="{TemplateBinding Property1}" />
    						<TextBlock Text="{TemplateBinding Property2}" />
    						<Rectangle Height="1" Fill="Black" HorizontalAlignment="Stretch" />
    						<TextBlock Text="{TemplateBinding Content}" />
    					</StackPanel>
    				</ControlTemplate>
    			</Setter.Value>
    		</Setter>
    	</Style>
    </Page.Resources>
    
    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    	<TextBlock Text="Hello, world!" Margin="20" FontSize="30" />
    	<local:TestControl Header="test1: property2 via attribute" Property1="1" Property2="2" Margin="4" Style="{StaticResource TestControlStyle}" />
    	<local:TestControl Header="test2: property2 via style setter" Property1="1" Margin="4" Style="{StaticResource TestControlStyleWithSetter}" />
    </StackPanel>
  3. changed the title [-][any-plat] dp changed callback not firing when set via style setter[/-] [+][any-plat] DependencyProperty changed callback not firing when set via style setter[/+] on Jun 18, 2020
  4. added
    project/styling 👔
    and removed
    triage/untriaged
    on Jun 18, 2020
  5. davidjohnoliver commented on Jun 18, 2020

    @davidjohnoliver
    Contributor

    Actually the callback is getting fired, but the Content property is not being updated... I suspect this is a problem with DP precedence being overridden while styles are being applied

  6. davidjohnoliver commented on Jun 18, 2020

    @davidjohnoliver
    Contributor

    And actually the precedence limitation was present on 2.4.

    The difference after the Styles update is that the styled property is now applied after the unstyled property (correctly, this is what UWP does). Formerly the styled property was applied first.

    So in this method:
    private void AddLine(string text)
    {
    Content = string.IsNullOrEmpty(Content?.ToString())
    ? text
    : Content + "\n" + text;
    }

    On 2.4, OnProperty2Changed sets Content at ExplicitStyle precedence, then OnProperty1Changed appends to it at Local precedence.

    On 3.0.0-dev.593, OnProperty1Changed sets Content at Local precedence, then OnProperty2Changed appends to it at ExplicitStyle precedence. However the appended value is masked because it's at a lower precedence.

  7. dr1rrb commented on Jun 19, 2020

    @dr1rrb
    Member

    This issue is also certainly causing the issue described here : #190 (comment)

    If we set the Template = DisplayTemplate it should override the template defined in the style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

kind/bugplatform/android 🤖platform/ios 🍎platform/wasm 🌐project/styling 👔

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions