文本框的可见性在WPF TabItem Header中没有改变

本文关键字:Header 改变 TabItem WPF 可见性 文本 | 更新日期: 2023-09-27 18:07:32

我已经为TabItem创建了一个样式。当双击tabItem header时,会出现一个文本框,以便编辑标题文本。

我想隐藏的文本框,当一个选项卡选择被改变或标签失去焦点,所以我设置可见性为false时,IsSelected为false或当SelectionChanged事件被触发,但它不工作。

<Trigger Property="IsSelected" Value="false">
    <Setter Property="Visibility" Value="{x:Static Visibility.Collapsed}" TargetName="tabHeaderEdit" />
    <Setter Property="Visibility" Value="{x:Static Visibility.Visible}" TargetName="tabHeaderPresenter" />
    <Setter TargetName="tabDockPanel" Property="Background" Value="#FFC9C7BA" />
</Trigger>
<EventTrigger RoutedEvent="Selector.SelectionChanged">
    <BeginStoryboard Storyboard="{StaticResource endHeaderEdit}"/>
</EventTrigger>

这里是完整的XAML。谁能告诉我这张试卷有什么问题吗?

<Window x:Class="WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic" 
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Window1" Height="300" Width="300">
<Window.Resources>
    <Storyboard x:Key="startHeaderEdit">
        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="tabHeaderEdit" Storyboard.TargetProperty="(UIElement.Visibility)">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="tabHeaderPresenter" Storyboard.TargetProperty="(UIElement.Visibility)">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="endHeaderEdit">
        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="tabHeaderEdit" Storyboard.TargetProperty="(UIElement.Visibility)" >
            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="tabHeaderPresenter" Storyboard.TargetProperty="(UIElement.Visibility)">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
    <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
    <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
    <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
    <Style x:Key="{x:Type TabItem}" TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">           
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">                        
                    <theme:ClassicBorderDecorator Name="ClassicBorder" SnapsToDevicePixels="true" BorderStyle="TabTop" Background="{TemplateBinding Background}" BorderBrush="{x:Static theme:ClassicBorderDecorator.ClassicBorderBrush}" BorderThickness="2">
                        <DockPanel x:Name="tabDockPanel">
                            <Button x:Name="tabCloseButton" Content="X" Cursor="Hand" DockPanel.Dock="Right" Focusable="False" FontFamily="Courier" FontSize="9" FontWeight="Bold" Margin="5,2,3,2" Padding="0" VerticalContentAlignment="Bottom" Width="16" Height="16"/>
                            <TextBox x:Name="tabHeaderEdit" DockPanel.Dock="Left" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Header}" Visibility="Collapsed">                                    
                            </TextBox>
                            <ContentPresenter x:Name="tabHeaderPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="true" HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" VerticalAlignment="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                        </DockPanel>
                    </theme:ClassicBorderDecorator>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Panel.ZIndex" Value="1" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="false">
                            <Setter Property="Visibility" Value="{x:Static Visibility.Collapsed}" TargetName="tabHeaderEdit" />
                            <Setter Property="Visibility" Value="{x:Static Visibility.Visible}" TargetName="tabHeaderPresenter" />
                            <Setter TargetName="tabDockPanel" Property="Background" Value="#FFC9C7BA" />
                        </Trigger>
                        <EventTrigger RoutedEvent="Control.MouseDoubleClick">
                            <BeginStoryboard Storyboard="{StaticResource startHeaderEdit}"/>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Selector.SelectionChanged">
                            <BeginStoryboard Storyboard="{StaticResource endHeaderEdit}"/>
                        </EventTrigger>
                        <!--<EventTrigger RoutedEvent="Mouse.MouseLeave">
                            <BeginStoryboard Storyboard="{StaticResource endHeaderEdit}"/>
                        </EventTrigger>-->
                        <Trigger Property="TabStripPlacement" Value="Bottom">
                            <Setter TargetName="ClassicBorder" Property="BorderStyle" Value="TabBottom" />
                        </Trigger>
                        <Trigger Property="TabStripPlacement" Value="Left">
                            <Setter TargetName="ClassicBorder" Property="BorderStyle" Value="TabLeft" />
                        </Trigger>
                        <Trigger Property="TabStripPlacement" Value="Right">
                            <Setter TargetName="ClassicBorder" Property="BorderStyle" Value="TabRight" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true" />
                                <Condition Property="TabStripPlacement" Value="Top"  />
                            </MultiTrigger.Conditions>
                            <Setter Property="Margin" Value="-2" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true" />
                                <Condition Property="TabStripPlacement" Value="Bottom" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Margin" Value="-2" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true" />
                                <Condition Property="TabStripPlacement" Value="Left" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Padding" Value="11,2,14,2" />
                            <Setter Property="Margin" Value="-2" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true" />
                                <Condition Property="TabStripPlacement" Value="Right" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Padding" Value="14,2,11,2" />
                            <Setter Property="Margin" Value="-2" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Value="{DynamicResource {x:Static SystemColors.GrayTextBrush}}" Property="Foreground" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>        
</Window.Resources>
<TabControl Name="tabControlView" Margin="4" BorderThickness="1" BorderBrush="Black">
    <TabControl.Items>
        <TabItem Header="Header 1" Content="Content 1" HorizontalAlignment="Left" VerticalAlignment="Top"></TabItem>
        <TabItem Header="Header 2" Content="Content 2"  HorizontalAlignment="Left" VerticalAlignment="Top"></TabItem>
        <TabItem Header="Header 3" Content="Content 3"  HorizontalAlignment="Left" VerticalAlignment="Top"></TabItem>
    </TabControl.Items>
</TabControl>

文本框的可见性在WPF TabItem Header中没有改变

如何停止编辑LostFocus:

<EventTrigger SourceName="tabHeaderEdit" RoutedEvent="UIElement.LostFocus">
    <BeginStoryboard Storyboard="{StaticResource endHeaderEdit}"/>
</EventTrigger>

(至少这是可行的,而且可能更符合通常的做法)

Edit: SelectionChanged触发器永远不会触发,因为事件属于TabControl而不是被模板化的TabItem,进一步的Setters不能改变值,因为它们已经被故事板动画了。

<Trigger Property="IsSelected" Value="false">
    <Trigger.EnterActions>
        <StopStoryboard BeginStoryboardName="start"/>
        <BeginStoryboard Name="end" Storyboard="{StaticResource endHeaderEdit}"/>
    </Trigger.EnterActions>
    <Setter TargetName="tabDockPanel" Property="Background" Value="#FFC9C7BA" />
</Trigger>
<EventTrigger RoutedEvent="Control.MouseDoubleClick">
    <BeginStoryboard Name="start" Storyboard="{StaticResource startHeaderEdit}"/>
</EventTrigger>

我仍然建议使用第一种方法,这是有点恼人的,如果文本框保持即使点击离开后,而停留在同一选项卡。