TabItem - 如何从TabItem样式触发器访问自定义HeaderTemplate中的元素

时间:2012-03-20 17:57:46

标签: wpf datatemplate tabcontrol tabitem

我有以下代码:

<Window x:Class="kkk.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">
    <Window.Resources>

        <Style TargetType="{x:Type TabItem}" x:Key="maintemplate">

            <Style.Resources>
                <DataTemplate x:Key="headertemplate" DataType="{x:Type TabItem}">
                    <StackPanel Orientation="Horizontal">
                        <ContentPresenter Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Header}"/>
                        <Button Content="X" x:Name="CloseButton">
                            <Button.LayoutTransform>
                                <ScaleTransform ScaleX="0.8" ScaleY="0.5"></ScaleTransform>
                            </Button.LayoutTransform>
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </Style.Resources>

            <Setter Property="HeaderTemplate" Value="{StaticResource headertemplate}"></Setter>

        </Style>

    </Window.Resources>
    <Grid>

        <TabControl>
            <TabItem Header="tab1" Style="{StaticResource maintemplate}"></TabItem>
            <TabItem Header="tab1" Style="{StaticResource maintemplate}"></TabItem>
        </TabControl>

    </Grid>
</Window>

它在每个tabitem标题上创建带有小x按钮的tabcontrol(它将用于关闭标签)。 现在我想在未选中选项卡时隐藏此按钮。 我尝试过类似的事情:

<Style.Triggers>
                <Trigger Property="IsSelected" Value="False">
                    <Setter Property="????" Value="Visible"></Setter>
                </Trigger>
            </Style.Triggers>

但我不知道如何从TabItem样式触发器访问CloseButton - &gt;我试过TargetName="CloseButton",但它不起作用......

2 个答案:

答案 0 :(得分:3)

我会在Button而不是TabItem上设置样式触发器,并使用RelativeSource绑定获取TabItem.IsSelected

<Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Visibility" Value="Visible"></Setter>
    <Style.Triggers>
        <DataTrigger Property="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}" Value="True">
            <Setter Property="Visibility" Value="Visible"></Setter>
        </DataTrigger >
    </Style.Triggers>
</Style>

答案 1 :(得分:1)

<Style TargetType="{x:Type TabItem}" x:Key="maintemplate">
    <Style.Resources>
        <DataTemplate x:Key="headertemplate" DataType="{x:Type TabItem}">
            <StackPanel Orientation="Horizontal">
                <StackPanel.Resources>
                    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
                </StackPanel.Resources>
                <ContentPresenter Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Header}"/>
                <Button Content="X" x:Name="CloseButton" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TabItem}},Path=IsSelected,Converter={StaticResource BoolToVisibilityConverter}}">
                    <Button.LayoutTransform>
                        <ScaleTransform ScaleX="0.8" ScaleY="0.5"></ScaleTransform>
                    </Button.LayoutTransform>
                </Button>
            </StackPanel>
        </DataTemplate>
    </Style.Resources>
    <Setter Property="HeaderTemplate" Value="{StaticResource headertemplate}"></Setter>
</Style>

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var isVisible = value as bool?;
        if (isVisible == null || !isVisible.Value)
        {
            return Visibility.Collapsed;
        }
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}