Repository navigation
Open
Labels
Milestone
Description
- .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] ";
}
}
}Reacted by Fabien Barbier
Activity
lnicolae commentedon Jun 18, 2020
In my case Loaded event is not raise if Visibility="Hidden", if I change property to Visibility="Collapsed", then event is called
hexawyz commentedon Sep 21, 2026
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
Loadedeverytime 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,RegisterClassHandlerwould 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
RegisterClassHandlernot working here, though. One can just manually bind thestaticevent handler they would have used forRegisterClassHandlerin 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 sameRoutedEventHandlerinstance.Anyway, having
RegisterClassHandlerwork withUnloadedbut notLoadedcreates a weird and surprising asymmetry. As long as we don't expose anOnLoadedmethods onFrameworkElement, this would still remain a relatively niche use case. And if people wish to break everything by binding a handler onFrameworkElement, 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 ofRegisterClassHandlerand emit a compilation warning.