【问题标题】:How to get the header of the selected item in a TabControl template?如何在 TabControl 模板中获取所选项目的标题?
【发布时间】:2013-04-23 09:36:49
【问题描述】:

我试图在我的TabControl 的模板中获取selected itemheader,但我做不到。我尝试了几种解决方案,但都没有奏效:

没有结果:

<ContentPresenter ContentSource="{TemplateBinding SelectedItem}"/>

编译错误(因为SelectedItem的类型是object,而不是HeaderedContentControl):

<ContentPresenter ContentSource="{TemplateBinding SelectedItem.Header}"/>

在 C# 中很容易得到它,但我想把它放在我的 TabControl 模板中。

有人知道吗?

谢谢

【问题讨论】:

    标签: wpf xaml header tabcontrol tabitem


    【解决方案1】:

    这应该可以解决问题:

    <TextBlock Text="{Binding SelectedItem.Header, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" />
    

    这是一个完整的 xaml 演示:

    <Window x:Class="WpfApplication21.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfApplication21"
                Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ControlTemplate x:Key="TabControlControlTemplate" TargetType="{x:Type TabControl}">
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Selected Item header: " />
                        <TextBlock Text="{Binding SelectedItem.Header, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" />
                    </StackPanel>
    
                    <Grid ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition x:Name="ColumnDefinition0"/>
                            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                            <RowDefinition x:Name="RowDefinition1" Height="*"/>
                        </Grid.RowDefinitions>
                        <TabPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="True" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                        <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                            <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </Grid>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="TabStripPlacement" Value="Bottom">
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                        <Setter Property="Margin" TargetName="HeaderPanel" Value="2,0,2,2"/>
                    </Trigger>
                    <Trigger Property="TabStripPlacement" Value="Left">
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                        <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                        <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                        <Setter Property="Margin" TargetName="HeaderPanel" Value="2,2,0,2"/>
                    </Trigger>
                    <Trigger Property="TabStripPlacement" Value="Right">
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                        <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                        <Setter Property="Margin" TargetName="HeaderPanel" Value="0,2,2,2"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
    
        </Window.Resources>
    
        <TabControl Template="{DynamicResource TabControlControlTemplate}">
            <TabItem Header="Tab 1" />
            <TabItem Header="Tab 2" />
            <TabItem Header="Tab 3" />
            <TabItem Header="Tab 4" />
        </TabControl>
    </Window>
    

    【讨论】:

    • 谢谢,它有效!但是,如果您将 Control 放在 TabItem 的标题中(如图像、面板),则 TextBlock 无法帮助您。我尝试了 ContentPresenter 和 ContentControl,但没有用...有什么想法吗?
    • 不客气。您可以使用 ContentControl :
    • 当我尝试将 WrapPanel 放在我的标题中时,如果我在模板中使用 ContentControl,我可以在“所选项目标题:”之后看到它,但它从 TabPanel 中消失了。很奇怪……
    • 这里有同样的问题。显然,直接绑定到 UIElement 会将其从 VisualTree 的源中删除。绑定到 ViewModel 并应用 DataTemplate 可能是更好的选择。
    【解决方案2】:

    我试图完成类似的事情,并且能够通过使用 Tag 而不是 Header 属性(其中包含一个控件)来解决 cmets 中提到的问题,并且能够使用上述解决方案做到这一点。

    我将所需的文本放入 TabItem 的标签中,并在我的模板中显示。希望这对某人有所帮助。

    <TextBlock  Text="{Binding SelectedItem.Tag, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}">
    

    ...

    <TabItem Tag="Header">
        <TabItem.Header>
            <Grid>...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 2012-09-17
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多