单击中键关闭TabItem

时间:2011-09-24 00:14:15

标签: wpf mvvm tabcontrol

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

3 个答案:

答案 0 :(得分: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>

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

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

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

答案 1 :(得分:0)

您可以向某些MouseBinding添加InputBindings吗?

答案 2 :(得分: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(matches); TabItem(即使未选中)。