C#WPFのタブコントロールに閉じるボタンを追加するサンプルコード
XAML内でリソース定義をします。 <Window.Resources> <Style TargetType="{x:Type TabItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Border Name="Border" Background="LightGray" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="4,4,0,0" Margin="2,0"> <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="10,2"/> </Border> <Button x:Name="closeButton" Content="X" Click="CloseButton_Click" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,-5,-5,0" Padding="3,0"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="Border" Property="Background" Value="White"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources>閉じるボタンクリック時のイベント動作を定義します。 private void CloseButton_Click(object sender, RoutedEventArgs e) { if (sender is Button closeButton) { if (closeButton.TemplatedParent is TabItem tabItem) { TabControl tabControl = (TabControl)tabItem.Parent; if (tabControl != null) { tabControl.Items.Remove(tabItem); } } } }全体のソースコードusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;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;namespace TabSample001{ /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void CloseButton_Click(object sender, RoutedEventArgs e) { if (sender is Button closeButton) { if (closeButton.TemplatedParent is TabItem tabItem) { TabControl tabControl = (TabControl)tabItem.Parent; if (tabControl != null) { tabControl.Items.Remove(tabItem); } } } } }}<Window x:Class="TabSample001.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <Style TargetType="{x:Type TabItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Border Name="Border" Background="LightGray" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="4,4,0,0" Margin="2,0"> <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="10,2"/> </Border> <Button x:Name="closeButton" Content="X" Click="CloseButton_Click" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,-5,-5,0" Padding="3,0"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="Border" Property="Background" Value="White"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <TabControl> <TabItem Header="Tab 1"> <Grid> <TextBlock Text="Content of Tab 1" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </TabItem> <TabItem Header="Tab 2"> <Grid> <TextBlock Text="Content of Tab 2" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </TabItem> <TabItem Header="Tab 3"> <Grid> <TextBlock Text="Content of Tab 3" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </TabItem> </TabControl> </Grid></Window>