单击中键关闭TabItem

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单击中键关闭TabItem相关的知识,希望对你有一定的参考价值。

我有个问题。在我的WPF应用程序中,如果我用鼠标中键按下tabItem,这个tabItem应该关闭。就像在FireFox中一样。但我尝试使用MVVM来做这件事,我需要使用命令。我的tabItem也是动态创建的。帮帮我吧!谢谢!

答案

为您的标签项创建一个DataTemplate,如下所示:

<DataTemplate x:Key="ClosableTabTemplate">
  <Border>
    <Grid>
      <Grid.InputBindings>
         <MouseBinding Command="ApplicationCommands.Close" Gesture="MiddleClick" />
      </Grid.InputBindings>
      <!-- the actual contents of your tab item -->
    </Grid>
  </Border>
</DataTemplate>

在应用程序窗口中,添加close命令

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Close" Executed="CloseCommandExecuted" CanExecute="CloseCommandCanExecute" />
</Window.CommandBindings>

最后将数据模板作为项目模板分配给选项卡控件。

另一答案

你可以添加一个MouseBinding到某些InputBindings

另一答案

关闭TabItem(未选中) - TabControl

<TabControl x:Name="TabControlUser" ItemsSource="{Binding Tabs}" Grid.RowSpan="3">
                <TabControl.Resources>
                    <Style TargetType="TabItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="TabItem">
                                    <Border Name="Border" BorderThickness="1,1,1,0" BorderBrush="Gainsboro" CornerRadius="4,4,0,0" Margin="2,0">
                                        <ContentPresenter x:Name="ContentSite"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center"
                                    ContentSource="Header"
                                    Margin="10,2"/>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter TargetName="Border" Property="Background" Value="LightSkyBlue" />
                                        </Trigger>
                                        <Trigger Property="IsSelected" Value="False">
                                            <Setter TargetName="Border" Property="Background" Value="GhostWhite" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TabControl.Resources>
                <TabControl.ItemTemplate>
                    <!-- this is the header template-->
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Header}" FontWeight="ExtraBold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <Image Source="/Images/RedClose.png" Width="22" Height="22" MouseDown="Image_MouseDown" HorizontalAlignment="Right" Margin="10 0 0 0"/>
                        </StackPanel>
                    </DataTemplate>
                </TabControl.ItemTemplate>
                <TabControl.ContentTemplate>
                    <!-- this is the body of the TabItem template-->
                    <DataTemplate>
                        <UserControl Content="{Binding UserControl, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>

C#代码 - 图像MouseDown单击事件

try
        {
            int matches = 0;
            DependencyObject dependency = (DependencyObject)e.OriginalSource;
            // Traverse the visual tree looking for TabItem
            while ((dependency != null) && !(dependency is TabItem))
                dependency = VisualTreeHelper.GetParent(dependency);
            if (dependency == null)
            {
                // Didn't find TabItem
                return;
            }
            TabItem selectedTabItem = dependency as TabItem;
            var selectedTabContent = selectedTabItem.Content as MainWindowViewModel.TabItem;
            foreach (var item in MainWindowViewModel.Tabs)
            {
                if (item.Header == selectedTabContent.Header)
                {
                    matches = MainWindowViewModel.Tabs.IndexOf(item);
                }
            }
            MainWindowViewModel.Tabs.RemoveAt(matches);
        }
        catch (Exception ex)
        {
            System.Windows.Application.Current.Dispatcher.Invoke((System.Action)(() => new View.MessageBox(ex.Message).ShowDialog()));
        }

此事件找到精确的鼠标单击位置(如TabItem),这将删除MainWindowViewModel.Tabs.RemoveAt(匹配); TabItem(即使未选中)。

以上是关于单击中键关闭TabItem的主要内容,如果未能解决你的问题,请参考以下文章

如何在AngularJS按钮单击中按按钮值过滤数据?

如何在 Compose 函数的按钮单击中启动 Intent

Delphi TTabControl在TabItem添加关闭按钮

在自定义按钮单击中播放 iframe 内的 youtube 视频

java.lang.IllegalStateException:键 f0 的片段不再存在:索引 1

WPF自适应可关闭的TabControl 类似浏览器的标签页