TabControl disposes of controls on inactive tabs

时光怂恿深爱的人放手 提交于 2019-11-30 10:22:25

This is a function of the TabControl and is the default behavior.

Basically, to save memory, the TabControl unloads the visual tree that is in its content area and replaces it with a newly crufted up one for the new tab. To prove this to yourself, you can listen to the Unload event on each control you template in and notice that it fires every time you switch tabs.

There are likely 2 reasons you would want to override this behavior:

  1. You believe that there would be a significant performance penalty.
  2. You are losing the state of the controls because any visual state that is being lost is not backed by a ViewModel.

As for #1, you shouldn't be concerned. CPU time is generally cheaper than RAM and the default behavior leans on the cheaper side of the resource equation. If you still feel like you REALLY don't want this behavior, you can see an example of overriding it here: https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf.Example/Controls/NonReloadingTabControl.cs

However, I would consider this a "smell" for potentially a future performance issue you should spend the time figuring out now, rather than delaying figuring it out.

For #2, you have two options:

  1. Make sure every property you want preserved (like IsSelected, etc) is backed by a ViewModel that preserves that state.
  2. Create a persistent UserControl for each tab that you bind to, rather than to ViewModels (Workspaces in your case). There is an example of that in the "Writer" sample for WAF: http://waf.codeplex.com/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!