TabItem无效的VisualStateManager.GoToState

时间:2012-01-31 22:44:08

标签: wpf xaml

我的tabitem由一个ResourceDictionary设计,它有一个名为Flashing的VisualState。 我想做这样的事情,但它不起作用。

public static void FlashTab(TabItem tbi)
{
  bool thisReturnsFalse =   VisualStateManager.GoToState(tbi, "Flashing", true);
}

XAML

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Color x:Key="TabBrush0">#7F605F5F</Color>
    <Color x:Key="TabBrush01">#7F605F5F</Color>
    <Color x:Key="TabBrush02">#7F605F5F</Color>
    <Color x:Key="TabBrush03">#7F605F5F</Color>
    <Color x:Key="TabBrush04">#7F605F5F</Color>


    <Color x:Key="TabBrush1">#7F7E7E7E</Color>
    <Color x:Key="TabBrush2">#7FFDFDFD</Color>
    <Color x:Key="TabBrush3">#7FFFFFFF</Color>
    <Color x:Key="TabBrush4">#BFFFFFFF</Color>
    <Color x:Key="TabBrush5">#F2FFFFFF</Color>
    <Color x:Key="TabBrush6">#FF6096E5</Color>
    <Color x:Key="TabBrush7">#FF9B7C35</Color>
    <Color x:Key="TabBrush8">#FFB0BAA1</Color>
    <Color x:Key="TabBrush10">White</Color>

    <Color x:Key="Foreground">#FF000000</Color>

    <Color x:Key="MouseOverColor">Purple</Color>
    <Color x:Key="SelectedColor">Blue</Color>

    <SolidColorBrush x:Key="ForegroundBrush" Color="{DynamicResource Foreground}" />




    <Style TargetType="{x:Type TabItem}"  >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid Margin="1,0,1,0" >
                        <VisualStateManager.VisualStateGroups>

                            <VisualStateGroup x:Name="CommonStates">

                                <VisualState x:Name="Normal"/>


                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="InnerRectangleMouseOver">
                                            <EasingDoubleKeyFrame KeyTime="0" Value=".2"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>

                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="InnerRectangleSelected">
                                            <EasingDoubleKeyFrame KeyTime="0" Value=".2"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>


                                <VisualState x:Name="Flashing" >
                                    <Storyboard>
                                        <DoubleAnimation 
                                            Storyboard.TargetName="GlassCube"
                                            Storyboard.TargetProperty="Opacity"
                                            From="0.0"
                                            To="1.0"                     
                                            Duration="0:0:0.33"
                                            AutoReverse="True"
                                            RepeatBehavior="Forever"/>
                                    </Storyboard>
                                </VisualState>


                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>


                        </VisualStateManager.VisualStateGroups>

                        <Rectangle x:Name="InnerRectangleMouseOver" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="0">
                            <Rectangle.Effect>
                                <BlurEffect/>
                            </Rectangle.Effect>

                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource MouseOverColor}" Offset="0.228"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>

                        <Rectangle x:Name="InnerRectangleSelected" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="0">
                            <Rectangle.Effect>
                                <BlurEffect/>
                            </Rectangle.Effect>

                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource SelectedColor}" Offset="0.228"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>






                        <Rectangle x:Name="InnerRectangle" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" >
                            <Rectangle.Effect>
                                <BlurEffect/>
                            </Rectangle.Effect>
                            <Rectangle.Stroke>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource Brush1}" Offset="1"/>
                                    <GradientStop Color="{DynamicResource Brush0}"/>
                                </LinearGradientBrush>
                            </Rectangle.Stroke>
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource Brush13}" Offset="0.228"/>
                                    <GradientStop Color="{DynamicResource Brush7}"/>
                                    <GradientStop Color="{DynamicResource Brush7}" Offset="0.823"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>


                        <Rectangle x:Name="GlassCube" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="1">
                            <Rectangle.Stroke>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource Brush4}"/>
                                    <GradientStop Color="{DynamicResource Brush4}"/>
                                    <GradientStop Color="{DynamicResource Brush6}" Offset="0.254"/>
                                    <GradientStop Color="{DynamicResource Brush5}" Offset="0.401"/>
                                </LinearGradientBrush>
                            </Rectangle.Stroke>
                            <Rectangle.OpacityMask>
                                <RadialGradientBrush>
                                    <GradientStop Color="{DynamicResource Brush11}" Offset="0"/>
                                    <GradientStop Color="{DynamicResource Brush13}" Offset="1"/>
                                </RadialGradientBrush>
                            </Rectangle.OpacityMask>
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource Brush12}"/>
                                    <GradientStop Color="{DynamicResource Brush7}" Offset="0.47"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>

                        <ContentPresenter HorizontalAlignment="Center" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" ContentSource="Header" >
                            <TextBlock.Foreground>
                                <SolidColorBrush Color="{DynamicResource Foreground}"/>
                            </TextBlock.Foreground>
                        </ContentPresenter>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



</ResourceDictionary>

1 个答案:

答案 0 :(得分:2)

当您将ResourceDictionary合并到TabControl的父窗口的Window.Resources时,TabItem将闪烁(ResourceDictionary在ResourceDictionary.xaml这里):

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

当您修复GlassCube矩形的OpacityMask时。我已经评论过了,它很高兴闪现:

<Rectangle x:Name="GlassCube" Margin="1,1,1,1" RadiusY="5" RadiusX="5" StrokeThickness="3" Opacity="1">
    <Rectangle.Stroke>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="{DynamicResource TabBrush4}"/>
            <GradientStop Color="{DynamicResource TabBrush4}"/>
            <GradientStop Color="{DynamicResource TabBrush6}" Offset="0.254"/>
            <GradientStop Color="{DynamicResource TabBrush5}" Offset="0.401"/>
        </LinearGradientBrush>
    </Rectangle.Stroke>
    <!--<Rectangle.OpacityMask>
        <RadialGradientBrush>
            <GradientStop Color="{DynamicResource TabBrush11}" Offset="0"/>
            <GradientStop Color="{DynamicResource TabBrush13}" Offset="1"/>
        </RadialGradientBrush>
    </Rectangle.OpacityMask>-->
    <Rectangle.Fill>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="{DynamicResource TabBrush12}"/>
            <GradientStop Color="{DynamicResource TabBrush7}" Offset="0.47"/>
        </LinearGradientBrush>
    </Rectangle.Fill>
</Rectangle>
编辑:关于颜色资源的键,还有一件事需要提及。您可以使用键TabBrush1TabBrush2等来定义它们,但是当您在Color="{DynamicResource ...}"中使用它们时,始终使用键Brush1Brush2等。