简体   繁体   English

在C#中的tab控件上附加一个关闭按钮

[英]Attach a single close button on the tabcontrol in C#

How i can attach a single close button on the tabcontrol in C#. 我如何在C#中的tabcontrol上附加单个关闭按钮。 There is a many way to attach a close button individually on each tabpages but I want to attach only single(eg) we can see on microsoft visual stdio 2008. So Plz help me. 有很多方法可以分别在每个选项卡上附加一个关闭按钮,但是我只想附加一个(例如)我们可以在Microsoft Visual Stdio 2008上看到的按钮。所以Plz可以帮助我。

Here is a cheap way to do it, which might get you started: 这是一种廉价的方法,可能会让您入门:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <TabControl DockPanel.Dock="Top">
            <TabItem Header="Test1" />
            <TabItem Header="Test2" />
            <TabItem Header="Test3" />
            <TabItem Focusable="False">
                <TabItem.Header>
                    <Button Command="{Binding CloseTab}" Content="X" Width="21" HorizontalAlignment="Stretch" />
                </TabItem.Header>
            </TabItem>
        </TabControl>
    </DockPanel>
</Window>

You then are left to implement a public ICommand CloseTab field or property on your DataContext, and style the tab control to your liking. 然后,您需要在DataContext上实现public ICommand CloseTab字段或属性,并根据自己的喜好对选项卡控件进行样式设置。

选项卡项目标题中的“关闭”按钮

Edit: 编辑:

If you use this method: 如果使用此方法:

  • Wiring up the button is tricky. 接线按钮很棘手。 You have to be careful not to close the tab that contains the button 您必须注意不要关闭包含按钮的标签
  • This isn't well adapted to dynamically created tabs, because you have to ensure the close button is appended to the list 这不适用于动态创建的选项卡,因为您必须确保将“关闭”按钮附加到列表中
  • You have to figure out how to re-select the last selected tab, when you close the selected tab 关闭选定的标签时,您必须弄清楚如何重新选择上一个选定的标签
  • You'll also have weird behavior when tabs start to wrap 标签开始包装时,您还会有怪异的行为
  • The tab-stop behavior is hard to get right. 制表符停止行为很难正确解决。 You can't make the last TabItem focusable, since focus is used to determine what to close, but tabbing to the close button breaks the normal TabItem keyboard flow 您不能使最后一个TabItem焦点,因为焦点用于确定要关闭的内容,但是按Tab键关闭正常的TabItem键盘流程

I have come up with a style that makes the button look like a regular tab, with a bold X on it, which makes it visually more like IE8, and fixes the keyboard selection problem. 我想出了一种样式,该样式使按钮看起来像常规选项卡,并在其上带有粗体X ,这在外观上更像IE8,并解决了键盘选择问题。 But it is complicated, and this solution is complicated enough. 但这很复杂,而且这种解决方案也很复杂。

Ultimately, a close button on every tab jives better with the tab control's default behavior. 最终,每个选项卡上的关闭按钮都可以更好地使用选项卡控件的默认行为。 The only problem with that solution is that it takes up more space. 该解决方案的唯一问题是占用更多空间。 You could cheat and make the close button collapse until you mouse over the tab item, though that's sort of a user-experience no-no, unless you just shrink it. 您可以作弊并使关闭按钮折叠,直到将鼠标悬停在选项卡项目上为止,尽管这只是用户体验的禁忌,除非您只是缩小它。

If you are serious about following through with the separate close button, I suggest you look at this article, and adapt what they do for the scroll buttons to your close button: 如果您认真考虑使用单独的关闭按钮,建议您看一下本文,然后将其对滚动按钮所做的操作调整为您的关闭按钮:

http://www.blogs.intuidev.com/post/2010/02/10/TabControlStyling_PartThree.aspx http://www.blogs.intuidev.com/post/2010/02/10/TabControlStyling_PartThree.aspx

Ignore what they do for close buttons :) 忽略它们对关闭按钮的作用:)

是否可以将您的tabcontrol放在另一个容器控件上,并让该控件的关闭按钮完成这项工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM