This is my MainWindow code, which contains a TabControl
:
<Window x:Class="TabControlNS.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:TabControlNS"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl Visibility="Visible">
<TabItem Header="Item2">
</TabItem>
<TabItem Header="Item1">
<ContentControl Loaded="ContentControl_Loaded">
<local:View/>
</ContentControl>
</TabItem>
<TabItem Header="Item3"/>
<TabItem Header="Item4"/>
</TabControl>
</Grid>
</Window>
<UserControl x:Class="TabControlNS.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TabControlNS"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="grid">
</Grid>
</UserControl>
And here's the code behind:
public partial class View : UserControl
{
public System.Windows.Forms.Integration.WindowsFormsHost host;
public View()
{
InitializeComponent();
IsKeyboardFocusWithinChanged += View_IsKeyboardFocusWithinChanged;
host = new System.Windows.Forms.Integration.WindowsFormsHost();
var mtbDate = new System.Windows.Forms.MaskedTextBox("00/00/0000");
grid.Children.Add(host);
this.Loaded += View_Loaded;
this.Unloaded += View_Unloaded;
}
private double LoadMinusUnloadCount = 0;
private void View_Loaded(object sender, RoutedEventArgs e)
{
LoadMinusUnloadCount++;
}
private void View_Unloaded(object sender, RoutedEventArgs e)
{
LoadMinusUnloadCount--;
}
private void View_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine($"View.IsKeyboardFocusedWithin = {IsKeyboardFocusWithin}");
}
}
Since the local:View
control is embedded inside "Item1", then View_Loaded
will be called twice ( once when the Window is starting up, another when the "Item1" is coming to view). No Unloaded event has been called in between.
According to Same Bent of Microsoft, this is clearly a bug
I encourage you to report bugs about things that are clearly wrong, such as
- Unloaded events with no matching Loaded
- Two Loaded events with no intervening Unloaded
- Element clearly visible and active but no Loaded
Or is it? Can Microsoft make such an elementary bug with such a control? How to explain this?