TabControl views are not disposed
-
I have a WPF application which consists of a main window containing a TabControl whose items are populated dynamically from the model. This is a simplified version:
The view model class - MainWindowViewModel - has an observable collection called ViewModels:
private ObservableCollection _viewModels;
public ObservableCollection ViewModels { get { if (\_viewModels == null) { \_viewModels = new ObservableCollection(); } return \_viewModels; } }
The views are created thus:
public MainWindowViewModel()
{
_profileDesignerViewModel = new ViewModel.ProfileDesignerViewModel();
ViewModels.Add(_profileDesignerViewModel);
_imageCaptureViewModel = new ViewModel.ImageCaptureViewModel();
ViewModels.Add(_imageCaptureViewModel);
}So far so good. Unfortunately this has a peculiarity. A view is instantiated each time the user selects the corresponding tab. The problem is that the old view is not disposed until the application shuts down.
-
I have a WPF application which consists of a main window containing a TabControl whose items are populated dynamically from the model. This is a simplified version:
The view model class - MainWindowViewModel - has an observable collection called ViewModels:
private ObservableCollection _viewModels;
public ObservableCollection ViewModels { get { if (\_viewModels == null) { \_viewModels = new ObservableCollection(); } return \_viewModels; } }
The views are created thus:
public MainWindowViewModel()
{
_profileDesignerViewModel = new ViewModel.ProfileDesignerViewModel();
ViewModels.Add(_profileDesignerViewModel);
_imageCaptureViewModel = new ViewModel.ImageCaptureViewModel();
ViewModels.Add(_imageCaptureViewModel);
}So far so good. Unfortunately this has a peculiarity. A view is instantiated each time the user selects the corresponding tab. The problem is that the old view is not disposed until the application shuts down.
-
Thanks. I tried that code (from a different web site) some months back for another project and it did not work. However, after reading your post I found the one below which works: c# - Stop TabControl from recreating its children - Stack Overflow[^] It is the exact same control class as per your link, but with the addition of a style in the XAML. I cannot as yet see why the syle makes the difference to the behaviour. Thanks again, very helpful.