Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

[Feature] Make T for BehaviorBase<T> constraint to DependencyObject or FrameworkElement #3856

Open

Description

@Rosuavio

Describe the problem this feature would solve

BehaviorBase<T> supports a lot of our behaviors but as far as its own API for consumption it feels like I can be refined.
Currently T is constrained to UIElement which is more specific than Behavior<T>'s DependencyObject. So BehaviorBase<T> cannot be associated with any DependencyObject like Behavior making it usable in less scenarios.

On the other hand its behavior from its docs

  1. Whenever initially fails, this method will subscribe to to allow lazy initialization.

Only works with FrameworkElements but T is constrained to UIElement.

While the behavior

  1. Prevent duplicate initialization that can happen (prevent multiple OnAttached calls);

will work regardless of the type it feels like constraining T to UIElement is an awkward middle ground.

Describe the solution

Embrace the usefulness outside of the T is FrameworkElement case and make the class usable in more scenarios.

public abstract class BehaviorBase<T> : Behavior<T>
-        where T : UIElement
+        where T : DependencyObject

or

Embrace the behavior of the T is FrameworkElement case and make the class constrained to scenarios where both 1 and 2 work.

public abstract class BehaviorBase<T> : Behavior<T>
-        where T : UIElement
+        where T : FrameworkElement

Describe alternatives you've considered

Do nothing.

Additional context & Screenshots

Activity

  1. changed the title [-][Feature] Make T for BehaviorBase<T> constraint to `DependencyObject` or `FrameworkElement`[/-] [+][Feature] Make T for BehaviorBase<T> constraint to DependencyObject or FrameworkElement[/+] on Mar 17, 2021
  2. added this to the 7.1 milestone on Mar 17, 2021
  3. michael-hawker commented on Mar 17, 2021

    @michael-hawker
    Member

    Good points @RosarioPulella, let's worry about this discussion for 7.1 and focus on the 7.0.1 bugs first.

    I think the underlying Behaviors SDK has made improvements in this area as well compared to when we first started. So I think the proper solution would be to try removing our BehaviorBase class all together and if there are remaining issues, we should investigate patching the Behavior SDK itself.

    See that discussion here and the original PR over there which I think addressed that concern here.

  4. Rosuavio commented on Apr 1, 2021

    @Rosuavio
    ContributorAuthor

    @Sergio0694 what do you think, does one solution make more sense, or should we leave this as it is.

  5. Sergio0694 commented on Apr 1, 2021

    @Sergio0694
    Member

    Absolutely agree that the current UIElement constraint is a bit pointless, since it doesn't really add anything of value. Personally I wouldn't be able to recall off the top of my head a case where I've ever wanted to use a behavior on something that was not also a FrameworkElement, so the second choice seems fine to me, if the major point of the implementation is specifically to support those events for FrameworkElement-s (especially because otherwise those OnAssociatedObjectLoaded, Unloaded and SizeChanged events wouldn't really make sense in the first place) 😄

    A couple notes/questions while goign through the BehaviorBase<T> implementation (reference source below):

    https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/9b23e4f8711a463e02d1b8fad6a9537c1de21200/Microsoft.Toolkit.Uwp.UI.Behaviors/BehaviorBase.cs#L43-L55

    1. The XML docs say that one point of this class is that it avoids repeated initialization, as there's the HandleAttach method that calls Initialize, which is then overridden by the user, and that's only called once. But, the event handlers are still added every time and they're not cleared (no -= beforehand), so aren't we technically still potentially causing multiple handlers being added? In particular, shouldn't we be only adding these handlers the first time the attaching was successful?
    2. If the user is supposed to insert their logic into Initialize and Uninitialize and not here, then OnAttached and OnDetaching should be sealed, right?
    3. Shouldn't Initialize and Uninitialize be called On___ for naming consistency with protected virtual methods?
    4. We're always subscribing to SizeChanged, which is potentially expensive during resize operations, even if the inherited classes are not using it. Should we have some customization logic to let consumers specify which methods they want to support (as an optimization), or would that be overkill?
    5. If we want to restrict T to FrameworkElement, what about FrameworkElementBehavior<T> rather than BehaviorBase<T>? Would make the purpose of the type clearer for the name (ie. "some behavior specifically supporting FrameworkElement-s) rather than a generic "Base" version, which personally makes me think "but... isn't Behavior<T> a base class already?".

    Just some general thoughts, hope this helps! 🙂

  6. Rosuavio commented on Apr 1, 2021

    @Rosuavio
    ContributorAuthor

    @Sergio0694 I agree, changing it to FrameworkElement would be clearer. If you want I can update my PR taking into account your notes or if you want you can open one. I know your working on the source generators so let me know.

  7. Sergio0694 commented on Apr 1, 2021

    @Sergio0694
    Member

    I can make a PR if you'd like me to take care of this, sure!
    I do need something else to do when taking a break from the other PR after all ahah 😄

  8. self-assigned this
    on Apr 1, 2021
  9. Rosuavio commented on Apr 1, 2021

    @Rosuavio
    ContributorAuthor

    Sounds good, thanks! 😃

  10. michael-hawker commented on Apr 1, 2021

    @michael-hawker
    Member

    We may also want to just check if we need as much of the logic in BehaviorBase now that the underlying Behavior was fixed? Think I pointed that our here: #3856 (comment)

  11. Rosuavio commented on Apr 1, 2021

    @Rosuavio
    ContributorAuthor

    Think I pointed that our here: #3856 (comment)

    Sorry, totally forgot as I came back to this.

  12. Sergio0694 commented on Apr 1, 2021

    @Sergio0694
    Member

    Oh, I had missed that comment too (opened this issue from the notification so it just jumped to Rosario's comment below) 😄
    If Behavior<T> is fixed now (which by the way, great! 🎉) then yeah I agree with @michael-hawker we should probably just remove this base class entirely and leave it up to users to add their own specialized logic. That'd also completely fix the issue with extra overhead (point 4 in my list) with the various event handlers we have here 🙂

    Do we want to then make BehaviorBase<T> obsolete and do a refactor pass on the repo to remove its usages, if any?

  13. michael-hawker commented on Apr 20, 2021

    @michael-hawker
    Member

    @Sergio0694 @RosarioPulella I think we just need a quick test that the new Behavior<T> behaves as we expect, but otherwise, I agree that if we can simplify things it'd be great.

    Want to take a look at this sometime soon @RosarioPulella? Not sure if we can make a simple app to exhibit a previously 'bad' behavior we thought we were working around?

    I feel like this was one of those Legacy things that has just stuck around... 😋

  14. modified the milestones: 7.1, 7.2/8.0? on Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions