Skip to content

Class handler for Loaded event is not always called #1602

Open

Description

@weltkante
  • .NET Core Version: 3.0.0-preview8-28379-02
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

I want to document a "well known bug" so it can be fixed in the future (it had a rejected connect issue but the site is no longer available)

Problem description:
Registering a class handler for the Loaded event will not raise the event unless someone else also subscribes an instance handler on a particular instance. Class handlers for other events (e.g. Unloaded or Click) don't seem to have this problem.

Actual behavior:
Class handlers for the Loaded event are only raised if instance handlers are also subscribed.

Expected behavior:
Class handlers for the Loaded event should always be raised, like it is done for other events.

Minimal repro:

<Window x:Class="WpfAppBugRepro1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfAppBugRepro1"
        mc:Ignorable="d"
        SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        
        <Label Grid.Row="0" Content="Only Class Handler:" />
        <Label Grid.Row="1" Content="Only Instance Handler:" />
        <Label Grid.Row="2" Content="Class+Instance Handler:" />
        
        <local:LabelWithClassHandler x:Name="control1" Grid.Row="0" Grid.Column="1" />
        <Label x:Name="control2" Grid.Row="1" Grid.Column="1" Loaded="InstanceHandler" />
        <local:LabelWithClassHandler x:Name="control3" Grid.Row="2" Grid.Column="1" Loaded="InstanceHandler" />

    </Grid>
</Window>
using System.Windows;
using System.Windows.Controls;

namespace WpfAppBugRepro1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void InstanceHandler(object sender, RoutedEventArgs e)
        {
            var control = (Label)sender;
            control.Content = (string)control.Content + " [Instance Handler called] ";
        }
    }

    public class LabelWithClassHandler : Label
    {
        static LabelWithClassHandler()
        {
            EventManager.RegisterClassHandler(typeof(LabelWithClassHandler), LoadedEvent, new RoutedEventHandler(ClassHandler));
        }

        private static void ClassHandler(object sender, RoutedEventArgs e)
        {
            var control = (Label)sender;
            control.Content = (string)control.Content + " [Class Handler called] ";
        }
    }
}

Activity

  1. added this to the 5.0 milestone on Aug 13, 2019
  2. added
    Bug
    on Aug 13, 2019
  3. lnicolae commented on Jun 18, 2020

    @lnicolae

    In my case Loaded event is not raise if Visibility="Hidden", if I change property to Visibility="Collapsed", then event is called

  4. modified the milestones: 5.0.0, 7.0.0 on Aug 26, 2021
  5. hexawyz commented on Sep 21, 2026

    @hexawyz

    Stumbled on this bug while trying to investigate a (possibly unrelated) problem with a finalizer not being called on a class derived from FrameworkElement. I would like it see solved at some point if possible.

    I understand that calling Loaded everytime may have a cost (I assume that was the reason for not providing this), but on the other hand, if someone implementing a class needs to run specific code for all instances of that class, RegisterClassHandler would still be the best way to achieve that result, as the cost can not be avoided anyway, and binding an event on every instance creation is likely less performant.

    There is a workaround to RegisterClassHandler not working here, though. One can just manually bind the static event handler they would have used for RegisterClassHandler in the constructor of an instance. This will likely incurr some allocations (all the lazy event store allocation) due to the event logic, but it can at least reuse the same RoutedEventHandler instance.

    Anyway, having RegisterClassHandler work with Unloaded but not Loaded creates a weird and surprising asymmetry. As long as we don't expose an OnLoaded methods on FrameworkElement, this would still remain a relatively niche use case. And if people wish to break everything by binding a handler on FrameworkElement, let it be their wish. With the recent .NET versions, it would also not be very difficult to come up with an analyser to detect a too-broad use of RegisterClassHandler and emit a compilation warning.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NET FrameworkBug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions