WPF 学生成绩管理工具开发笔记(2)— 创建并使用带有关闭功能的TabItem控件

这篇博客介绍了如何在WPF应用中开发一个学生成绩管理工具,重点在于创建一个带有关闭功能的TabItem控件。通过自定义控件CloseableTabItem,注册CloseTab路由事件,以及在App.xaml和Shell.xaml.cs中进行相关配置,实现了TabItem的关闭功能。最后展示了在实际项目中的综合应用代码。
摘要由CSDN通过智能技术生成

  目录

一、Shell.Xaml前端代码

二、创建带有关闭功能的TabItem继承控件CloseableTabItem

1、选择自定件控件选项建立 CloseableTabItem 派生自TabItem,并注册一个名称为CloseTab 的路由事件

2、为CloseTabItem创建资源字典CloseableTabItemStyle.xaml

3、App.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=&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值