Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. TabControl views are not disposed

TabControl views are not disposed

Scheduled Pinned Locked Moved WPF
csharpwpfhelpannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leif Simon Goodwin
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • L Leif Simon Goodwin

      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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The WPF TabControl is well known for not persisting views. Fortunately, there are many implementations that work around this - Sacha Barber has one such here[^]

      This space for rent

      L 1 Reply Last reply
      0
      • P Pete OHanlon

        The WPF TabControl is well known for not persisting views. Fortunately, there are many implementations that work around this - Sacha Barber has one such here[^]

        This space for rent

        L Offline
        L Offline
        Leif Simon Goodwin
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups