2011-09-24 57 views
3

我遇到问题。 在我的WPF应用程序中,如果我用鼠标中键按TabItem,这个tabItem应该关闭。就像在FireFox中一样。 但我尝试使用MVVM来做到这一点,我需要使用命令。此外我的tabItems是动态创建的。 帮我看看! 谢谢!通过单击中间按钮来关闭TabItem

回答

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> 

最后的数据模板项目模板分配给你的标签控制。

+0

当我处理CloseCommandExecuted时,e.Source返回一个TabControl对象。我如何获得TabItem对象? – Brandon

0

您可以添加MouseBinding某些InputBindings也许?

+0

我的tabItems是动态创建的。我应该在代码后面添加一个mouseBinding吗? – Yevgeniy

+0

@Yevgeniy:这将是一个选项,不幸的是,这不能通过样式完成... –