I have a WPF Window containing a ContentControl that uses a DataTemplate which is defined in the windows' resources:
<Window (...)>
<Control.Resources>
<DataTemplate DataType="{x:Type local:MainVM}">
<local:MainView />
</DataTemplate>
</Control.Resources>
<ContentControl Content="{Binding}" />
<!--<local:MainView /> This Works as expected -->
</Window>
For testing, the MainView is a normal wpf usercontrol with no content.
In my MainWindow constructor I set the datacontext once:
public MainWindow()
{
InitializeComponent();
DataContext = new MainVM();
}
Normally, I expect the MainView constructor to be called exactly once if I never change the DataContext of my window.
However, it is called every time I log onto my machine using remote desktop or when I just press "Switch User" and relogin:
public MainView()
{
InitializeComponent(); // <--- this is called upon loggin on
}
Neither the window
DataContextChangedevent nor theMainView.Unloadedevent are called.This only happens on my Windows 10 Pro 64-bit v1511 machine, but not on a Windows 7 PC.
It also only happens if I use the
ContentControl, not if I just create the MainView directly.If the screen is just locked, it does not happen.
Does anyone have an idea why this is happening or how I can prevent that?
I have found other posts on StackOverflow that might address the same problem:
WPF: Prevent unload & load after RDP (dis)connect
WPF re-invoke constructor when connect using Remote Desktop Connection (RDP) to pc