单击中键关闭TabItem

问题描述 投票:2回答:3

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

wpf mvvm tabcontrol
3个回答
6
投票

为您的标签项创建一个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>

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


0
投票

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


0
投票

关闭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(即使未选中)。

© www.soinside.com 2019 - 2024. All rights reserved.