目录
二、创建带有关闭功能的TabItem继承控件CloseableTabItem
1、选择自定件控件选项建立 CloseableTabItem 派生自TabItem,并注册一个名称为CloseTab 的路由事件
2、为CloseTabItem创建资源字典CloseableTabItemStyle.xaml
4、Shell.xaml.cs中代码指定路由事件,并为CloseTab实现方法:
由于工具有多个Window窗口(或者UserControl),界面的切换统一利用TabControl来实施。由于Vs提供的TabItem缺少关闭功能,所以利用自定义控件来完成,最终效果如图
一、Shell.Xaml前端代码
<TabControl x:Name="mainTab" Grid.Row="2" VerticalContentAlignment="Center" >
<TabControl.Background>
<ImageBrush ImageSource="/学生成绩;component/Resources/ImgBack.jpg" Opacity="0.8"></ImageBrush>
</TabControl.Background>
</TabControl>
二、创建带有关闭功能的TabItem继承控件CloseableTabItem
1、选择自定件控件选项建立 CloseableTabItem 派生自TabItem,并注册一个名称为CloseTab 的路由事件
using System.Windows;
using System.Windows.Controls;
namespace ScoreTools.Views
{
public class CloseableTabItem : TabItem
{
// this.AddHandler(CloseableTabItem.CloseTabEvent, new RoutedEventHandler(this.CloseTab));
static CloseableTabItem()
{
//This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
//This style is defined in themes\generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem), new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
}
public static readonly RoutedEvent CloseTabEvent =EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(CloseableTabItem));
public event RoutedEventHandler CloseTab
{
add { AddHandler(CloseTabEvent, value); }
remove { RemoveHandler(CloseTabEvent, value); }
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Button closeButton = base.GetTemplateChild("PART_Close") as Button;
if (closeButton != null)
closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
}
void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
}
}
}
2、为CloseTabItem创建资源字典CloseableTabItemStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:ScoreTools.Views"
>
<Style x:Key="TabItemFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle SnapsToDevicePixels="true" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="3,3,3,1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="TabControlNormalBorderBrush" Color="#8C8E94"/>
<LinearGradientBrush x:Key="TabItemHotBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#EAF6FD" Offset="0.15"/>
<GradientStop Color="#D9F0FC" Offset=".5"/>
<GradientStop Color="#BEE6FD" Offset=".5"/>
<GradientStop Color="#A7D9F5" Offset="1"/>
</LinearGradientBrush>
<!--<SolidColorBrush x:Key="TabItemSelectedBackground" Color="#F9F9F9"/>-->
<SolidColorBrush x:Key="TabItemSelectedBackground" Color="Yellow"/>
<SolidColorBrush x:Key="TabItemSelectedForeground" Color="White"/>
<SolidColorBrush x:Key="TabItemHotBorderBrush" Color="#3C7FB1"/>
<SolidColorBrush x:Key="TabItemDisabledBackground" Color="#F4F4F4"/>
<SolidColorBrush x:Key="TabItemDisabledBorderBrush" Color=&#