单击中键关闭TabItem

Yev*_*niy 2 wpf tabcontrol mvvm

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

teh*_*exx 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>
Run Code Online (Sandbox Code Playgroud)

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

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Close" Executed="CloseCommandExecuted" CanExecute="CloseCommandCanExecute" />
</Window.CommandBindings>
Run Code Online (Sandbox Code Playgroud)

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