wpf - 如何在 WPF 中的模板化 TabItem 上获得关闭按钮?

标签 wpf .net-3.5 styles datatemplate

我有一个 TabControl,其中 TabItemDataTemplat 的。该模板似乎工作正常,因为我想在 TabItem 中显示的用户控件正确显示。

我不确定的是如何让“x”出现在 TabItem 中以便我可以关闭每个选项卡,因为它们是通过模板动态生成的。

作为 WPF 的新手,我开始了解许多概念,但是 TabControl 给我带来了很多麻烦,所以我很可能拥有可行的模板,但不是可维护。

这就是我所拥有的,我希望能够关闭每个 TabControl。我还需要能够在 TabControl 关闭时触发自定义事件。

<UserControl x:Class="Russound.Windows.UI.UserControls.CallLog.CaseReaderWpf"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:CallLog="clr-namespace:Russound.Windows.UI.UserControls.CallLog"
    Height="637" Width="505">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Russound.Windows;component/UI/RussoundDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <TabControl x:Name="tabCases" >
        <TabControl.ItemTemplate>
            <DataTemplate DataType="{x:Type TabItem}">
                <StackPanel>
                    <TextBlock Text="{Binding Path=Id}" />
                </StackPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate DataType="{x:Type TabItem}">
                <CallLog:CaseReadOnlyDisplay DataContext="{Binding}" />
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</UserControl>

最佳答案

查看 Josh Smith 撰写的这篇 MSDN 文章。对于您的问题,这是一个很好的解决方案。

WPF Apps With The Model-View-ViewModel Design Pattern

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

<!-- 
This template explains how to render 
a tab item with a close button.
-->
<DataTemplate x:Key="ClosableTabItemTemplate">
<DockPanel Width="120">
  <Button 
    Command="{Binding Path=CloseCommand}"
    Content="X"
    Cursor="Hand"
    DockPanel.Dock="Right"
    Focusable="False"
    FontFamily="Courier" 
    FontSize="9"
    FontWeight="Bold"  
    Margin="0,1,0,0"
    Padding="0"
    VerticalContentAlignment="Bottom"
    Width="16" Height="16" 
    />
  <ContentPresenter 
    Content="{Binding Path=DisplayName}" 
    VerticalAlignment="Center" 
    />
</DockPanel>
</DataTemplate>

<!--
This template explains how to render the 'Workspace' content area in the main window.
-->
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl 
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding}" 
  ItemTemplate="{StaticResource ClosableTabItemTemplate}"
  Margin="4"
  />
</DataTemplate>

关于wpf - 如何在 WPF 中的模板化 TabItem 上获得关闭按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/935043/

相关文章:

wpf - 是否可以使用WPF和Delphi 2007/09 win32?

wpf - 覆盖 TextBox.TextProperty 的默认值

html - 覆盖不透明样式

c# - .NET 3.5 中的 "An existing connection was forcibly closed by the remote host"

.net - Predicate<T> 在 .NET 3.0+ 中不可用吗

android - 如何在 Android 上更改默认的 ProgressBar 圆圈颜色

css - Highcharts - 在饼图中为一个楔形放置样式和更改样式的位置

c# - 使用 WPF 和 XAML 创建图像的滚动网格

c# - 管理按钮的 IsEnabled 属性

c# - 从正在等待的事件返回反馈