Skip to content

Class handler for Loaded event is not always called #1602

@weltkante

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] ";
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions