98 lines
3.5 KiB
C#
98 lines
3.5 KiB
C#
// Copyright (c) 2017 schick Informatik
|
|
// Description: Custom tab item
|
|
//
|
|
|
|
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Animation;
|
|
|
|
namespace ENI2.Controls
|
|
{
|
|
/// <summary>
|
|
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
|
|
///
|
|
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
|
|
/// Add this XmlNamespace attribute to the root element of the markup file where it is
|
|
/// to be used:
|
|
///
|
|
/// xmlns:MyNamespace="clr-namespace:ENI2.Controls"
|
|
///
|
|
///
|
|
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
|
|
/// Add this XmlNamespace attribute to the root element of the markup file where it is
|
|
/// to be used:
|
|
///
|
|
/// xmlns:MyNamespace="clr-namespace:ENI2.Controls;assembly=ENI2.Controls"
|
|
///
|
|
/// You will also need to add a project reference from the project where the XAML file lives
|
|
/// to this project and Rebuild to avoid compilation errors:
|
|
///
|
|
/// Right click on the target project in the Solution Explorer and
|
|
/// "Add Reference"->"Projects"->[Browse to and select this project]
|
|
///
|
|
///
|
|
/// Step 2)
|
|
/// Go ahead and use your control in the XAML file.
|
|
///
|
|
/// <MyNamespace:ClosableTabItem/>
|
|
///
|
|
/// </summary>
|
|
public class ClosableTabItem : TabItem
|
|
{
|
|
/*
|
|
static ClosableTabItem()
|
|
{
|
|
DefaultStyleKeyProperty.OverrideMetadata(typeof(ClosableTabItem), new FrameworkPropertyMetadata(typeof(ClosableTabItem)));
|
|
}
|
|
*/
|
|
|
|
public event EventHandler TabClosing;
|
|
|
|
|
|
public void SetHeaderText(string headerText, bool lockedTab)
|
|
{
|
|
// Container for header controls
|
|
var dockPanel = new DockPanel();
|
|
var header = new TextBlock { Text = headerText };
|
|
header.Background = lockedTab ? Brushes.LightGreen : Brushes.Orange;
|
|
dockPanel.Children.Add(header);
|
|
|
|
// Close button to remove the tab
|
|
var closeButton = new Button();
|
|
closeButton.Content = "X";
|
|
closeButton.Margin = new Thickness(5, 0, 0, 0);
|
|
closeButton.Foreground = Brushes.Red;
|
|
closeButton.Height = 18;
|
|
closeButton.Width = 15;
|
|
closeButton.Opacity = 0.2;
|
|
closeButton.FontSize = 8;
|
|
closeButton.FontWeight = FontWeights.Bold;
|
|
|
|
closeButton.MouseEnter += (sender, e) => {
|
|
DoubleAnimation animation = new DoubleAnimation(0.2, 1.0, new TimeSpan(0, 0, 0, 0, 500));
|
|
closeButton.BeginAnimation(Button.OpacityProperty, animation);
|
|
};
|
|
closeButton.MouseLeave += (sender, e) => {
|
|
DoubleAnimation animation = new DoubleAnimation(1.0, 0.2, new TimeSpan(0, 0, 0, 0, 500));
|
|
closeButton.BeginAnimation(Button.OpacityProperty, animation);
|
|
};
|
|
|
|
closeButton.Click +=
|
|
(sender, e) =>
|
|
{
|
|
var tabControl = Parent as ItemsControl;
|
|
tabControl.Items.Remove(this);
|
|
if (TabClosing != null)
|
|
this.TabClosing(this, new EventArgs());
|
|
};
|
|
dockPanel.Children.Add(closeButton);
|
|
|
|
// Set the header
|
|
Header = dockPanel;
|
|
}
|
|
|
|
}
|
|
}
|