Click here to Skip to main content
15,894,343 members
Articles / Desktop Programming / WPF

Persist the Visual Tree when switching tabs in the WPF TabControl (Optimized)

Rate me:
Please Sign up or sign in to vote.
4.83/5 (9 votes)
9 Apr 2012CPOL4 min read 50.8K   2.6K   15  
This is an alternative for "Persist the Visual Tree when switching tabs in the WPF TabControl".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.ComponentModel;

namespace NonVirtualizingTabControlDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            Items = new ObservableCollection<ColorInfo>();
            DataContext = this;
        }

        #region Fields

        private ColorInfo _current;

        #endregion

        #region Properties

        public ColorInfo SelectedColor
        {
            get { return _current; }
            set
            {
                _current = value;
                if (!(Items as ObservableCollection<ColorInfo>).Contains(value))
                    (Items as ObservableCollection<ColorInfo>).Add(value);

                PropertyChanged(this, new PropertyChangedEventArgs("SelectedColor"));
            }
        }

        public IEnumerable<ColorInfo> Items { get; private set; }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Senior Software Engineer with over 20+ years of experience in variety of technologies, development tools and programming languages.

Microsoft Certified Specialist programming in C#, JavaScript, HTML, CSS

Comments and Discussions