我试图在项目添加到ListBox时添加某种动画,因为这篇博客声称要做 Link
问题是,当我使用BeforeLoaded和Loaded状态名称添加VisualStateGroup时,项目在ListBox中根本无法呈现。当我悬停时,我得到蓝色突出显示,当我点击选择时,我得到更亮的蓝色,但是列表框项目(随机矩形)的实际内容不存在。
当我删除该组时,矩形呈现完美,但没有任何动画(显然)。附件是ListBox的ItemContainerStyle的完整样式。其余的代码是从链接中逐字记录的。
<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
<Setter Property="Padding" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="LayoutStates">
<VisualState x:Name="BeforeLoaded">
<Storyboard>
<DoubleAnimation
Duration="00:00:00"
By="-196"
Storyboard.TargetName="contentProjection"
Storyboard.TargetProperty="GlobalOffsetX" />
<DoubleAnimation
Duration="00:00:00"
By="-180"
Storyboard.TargetName="contentProjection"
Storyboard.TargetProperty="RotationY" />
<DoubleAnimation
Duration="00:00:00"
By="-270"
Storyboard.TargetName="contentProjection"
Storyboard.TargetProperty="RotationX" />
</Storyboard>
</VisualState>
<VisualState x:Name="Loaded">
<Storyboard>
<DoubleAnimation
Duration="00:00:01"
To="0"
Storyboard.TargetName="contentProjection"
Storyboard.TargetProperty="GlobalOffsetX" />
<DoubleAnimation
Duration="00:00:01"
To="0"
Storyboard.TargetName="contentProjection"
Storyboard.TargetProperty="RotationY" />
<DoubleAnimation
Duration="00:00:01"
To="0"
Storyboard.TargetName="contentProjection"
Storyboard.TargetProperty="RotationX" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unloaded" />
</VisualStateGroup>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="fillColor" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
<Rectangle x:Name="fillColor2" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
<!--<ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>-->
<ContentPresenter x:Name="contentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}">
<ContentPresenter.Projection>
<PlaneProjection x:Name="contentProjection">
</PlaneProjection>
</ContentPresenter.Projection>
</ContentPresenter>
<Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:2)
博客中的以下代码存在的问题之一是许多解释令人兴奋的新功能的博客是针对CTP和Beta版本编写的。因此,一些人不再对RTM版本进行逐字工作。
在这种情况下,您(和Mike)调用“已加载”的状态实际上称为“AfterLoaded”。