我有一个 TabControl
,其中 TabItem
是 DataTemplat
的。该模板似乎工作正常,因为我想在 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/