2010-12-20 75 views
2

我想某種動畫添加到項目,因爲它們可以添加到一個列表框,因爲這個博客聲稱做 Link動畫列表框項目在Silverlight

麻煩的是,當我加我VisualStateGroup使用我的BeforeLoaded和Loaded狀態名稱,項目在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

你讀過Petzold的文章http://msdn.microsoft.com/en-us/magazine/ff798276.aspx? – Denis 2010-12-21 18:29:49

+0

如果我仔細閱讀,我會發現安東尼在下面提到的錯字。謝謝。 – 2010-12-21 19:14:58

回答

2

一個在博客下面的代碼的問題是,許多博客解釋激動人心的新特性是針對CTP和Beta版本編寫的。因此,有些人停止對RTM版本進行逐字處理。

在這種情況下,您(和Mike)稱爲「Loaded」的狀態實際上稱爲「AfterLoaded」。

+0

謝謝。這是在MSDN的任何地方?我希望下次能夠節省一些麻煩。 – 2010-12-21 19:15:30

+1

@Adam:在http://msdn.microsoft.com/zh-cn/library/cc278062(v=VS.95).aspx請參閱表「ListBoxItem States」。 – AnthonyWJones 2010-12-21 21:18:32