2

我有一个s 被编辑的TabControl地方。该模板似乎工作正常,因为我想在其中显示的用户控件显示正确。TabItemDataTemplatTabItem

我不确定如何在其中显示“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>
4

5 回答 5

7

查看 Josh Smith 的这篇 MSDN 文章。这是您问题的绝佳解决方案。

具有模型-视图-视图模型设计模式的 WPF 应用程序

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>
于 2009-06-01T15:26:39.933 回答
0

您将不得不派生自己的选项卡控件。 谷歌搜索显示了许多结果,其中许多带有来源,因此您不必重新创建轮子。

于 2009-06-01T14:56:03.177 回答
0

Josh Smith 为 MSDN 杂志写了一篇文章,其中包含一个带有关闭按钮的选项卡项目的工作示例。该代码基于 MVVM 模式,但您应该能够从选项卡项控件模板中提取相关部分。

我没有 OpenID 登录,因此无法直接发布 URL。谷歌搜索“josh smith mvvm 演示应用程序”。

于 2009-06-01T15:09:39.813 回答
0

不要劫持线程,但您可能想考虑当每个选项卡都有一个关闭按钮时,事情看起来有多难看。如果您更喜欢集成到TabControl自身中的单个关闭按钮(la Visual Studio),您可以查看我所做的这篇博客文章,它作为示例的一部分(但不是文章的重点) )。

于 2009-06-01T15:20:52.923 回答
0

刚好碰到那个。我正在做 MVVM,但使用表单事件会非常相似。无论如何,我使用了 ItemContainerStyle 参数并将其指向具有数据类型限定符的样式,如下所示:

  <Style x:Key="TabHeader" TargetType="TabItem">
        <Setter Property="FieldLayoutSettings">
            <Setter.Value>
                <StackPanel Orientation="Horizontial">
                    <TextBlock Text="{Binding HeaderText}"/>
                    <!-- MVVM style -->
                    <Button Content="X" Command="{Binding [ICommandHere]}" />
                    <!--or... Forms style -->    
                    <Button Content="X" Click="EventHandlerHere" />
             </StackPanel>
            </Setter.Value>
            </Setter> 
    </Style>

<TabControl ItemsSource="{Binding Workspaces}"
            ItemContainerStyle="{StaticResource TabHeader}"/>
于 2009-11-05T20:33:19.140 回答