#region Copyright
// Diagnostic Explorer, a .Net diagnostic toolset
// Copyright (C) 2010 Cameron Elliot
//
// This file is part of Diagnostic Explorer.
//
// Diagnostic Explorer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Diagnostic Explorer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Diagnostic Explorer. If not, see <http://www.gnu.org/licenses/>.
//
// http://diagexplorer.sourceforge.net/
#endregion
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
namespace DiagnosticExplorer.Silverlight.Controls
{
public class CustomTabControl : TabControl
{
public CustomTabControl()
: base()
{
}
public DataTemplate TabHeaderItemTemplate
{
get { return (DataTemplate)GetValue(TabHeaderItemTemplateProperty); }
set { SetValue(TabHeaderItemTemplateProperty, value); }
}
public static readonly DependencyProperty TabHeaderItemTemplateProperty =
DependencyProperty.Register("TabHeaderItemTemplate", typeof(DataTemplate), typeof(CustomTabControl), new PropertyMetadata(
(sender, e) =>
{
((CustomTabControl)sender).InitTabs();
}));
public DataTemplate TabItemTemplate
{
get { return (DataTemplate)GetValue(TabItemTemplateProperty); }
set { SetValue(TabItemTemplateProperty, value); }
}
public static readonly DependencyProperty TabItemTemplateProperty =
DependencyProperty.Register("TabItemTemplate", typeof(DataTemplate), typeof(CustomTabControl), new PropertyMetadata(
(sender, e) =>
{
((CustomTabControl)sender).InitTabs();
}));
public IList MyItemsSource
{
get { return (IList)GetValue(MyItemsSourceProperty); }
set { SetValue(MyItemsSourceProperty, value); }
}
public static readonly DependencyProperty MyItemsSourceProperty =
DependencyProperty.Register("MyItemsSource", typeof(IList), typeof(CustomTabControl), new PropertyMetadata(
(sender, e) =>
{
((CustomTabControl)sender).HandleItemsSourceChanged(e);
}));
internal void HandleItemsSourceChanged(DependencyPropertyChangedEventArgs e)
{
try
{
INotifyCollectionChanged oldSource = e.OldValue as INotifyCollectionChanged;
if (oldSource != null)
oldSource.CollectionChanged -= HandleCollectionChanged;
INotifyCollectionChanged newSource = e.NewValue as INotifyCollectionChanged;
if (newSource != null)
newSource.CollectionChanged += HandleCollectionChanged;
InitTabs();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
try
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
AddItems(e.NewItems, e.NewStartingIndex);
break;
case NotifyCollectionChangedAction.Remove:
RemoveItems(e);
break;
case NotifyCollectionChangedAction.Replace:
ReplaceItems(e);
break;
case NotifyCollectionChangedAction.Reset:
InitTabs();
break;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private void AddItems(IList items, int startIndex)
{
int currentIndex = startIndex;
foreach (object obj in items)
{
TabItem newitem = new TabItem();
if (TabItemTemplate != null)
newitem.Content = TabItemTemplate.LoadContent();
if (TabHeaderItemTemplate != null)
newitem.Header = TabHeaderItemTemplate.LoadContent();
newitem.DataContext = obj;
Items.Insert(currentIndex++, newitem);
}
}
private void RemoveItems(NotifyCollectionChangedEventArgs e)
{
for (int i = 0; i < e.OldItems.Count; i++)
{
Items.RemoveAt(e.OldStartingIndex);
}
}
private void ReplaceItems(NotifyCollectionChangedEventArgs e)
{
for (int i = 0; i < e.OldItems.Count; i++)
{
TabItem item = (TabItem)Items[e.OldStartingIndex + i];
item.DataContext = e.NewItems[i];
}
}
internal void InitTabs()
{
Items.Clear();
if (MyItemsSource != null)
{
AddItems(MyItemsSource, 0);
}
}
}
}