2011-06-17 35 views
0

在我尝试和了解更多关于WPF的内容中,我一直在寻找一些控件的样式,我希望有人能够帮助我制作一些动画。如何结束扩展模板?

我一直在试图做的是采取一个扩展器,使其动画扩展/折叠,并在鼠标悬停时自动执行此操作。

我发现太网站分别说明这些技术,但我努力将它们结合起来,希望有人能帮助我完成。目前我无法获得动画效果,我不知道为什么。

Animation
AutoExpansion

我剥了下来很多动画造型的东西更简单,因为我不需要学习复杂的例子。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="640" Height="480"> 
    <Page.Resources> 
     <Style x:Key="ExpanderHeaderFocusVisual"> 
      <Setter Property="Control.Template"> 
       <Setter.Value> 
        <ControlTemplate> 
         <Border> 
          <Rectangle SnapsToDevicePixels="true" Margin="0" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <Style x:Key="ExpanderStyle1" TargetType="{x:Type Expander}"> 
      <Setter Property="Foreground" Value="Transparent"/> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
      <Setter Property="BorderBrush" Value="Transparent"/> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Expander}"> 
         <ControlTemplate.Resources> 
          <Storyboard x:Key="Timeline1"> 
           <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:0.25" Storyboard.TargetName="ExpandSite" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 
            <SplineDoubleKeyFrame KeyTime="00:00:0.25" Value="1"/> 
           </DoubleAnimationUsingKeyFrames> 
          </Storyboard> 
          <Storyboard x:Key="Timeline2"> 
           <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.45" Storyboard.TargetName="ExpandSite" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 
            <SplineDoubleKeyFrame KeyTime="00:00:0.45" Value="0"/> 
           </DoubleAnimationUsingKeyFrames> 
          </Storyboard> 
         </ControlTemplate.Resources> 
         <Border x:Name="border" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3"> 
          <DockPanel> 
           <ContentPresenter FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}" 
                 Visibility="Visible" 
                 Margin="{TemplateBinding Padding}" 
                 Focusable="False" 
                 x:Name="HeaderSite" 
                 Content="{TemplateBinding Header}" 
                 ContentTemplate="{TemplateBinding HeaderTemplate}" 
                 ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" 
                 DockPanel.Dock="Top"/> 
           <ContentPresenter Focusable="false" 
                  Visibility="Collapsed" 
                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                  Margin="{TemplateBinding Padding}" 
                  x:Name="ExpandSite" 
                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                  DockPanel.Dock="Bottom"> 
            <ContentPresenter.LayoutTransform> 
             <TransformGroup> 
              <ScaleTransform ScaleX="1" ScaleY="0"/> 
              <SkewTransform AngleX="0" AngleY="0"/> 
              <RotateTransform Angle="0"/> 
              <TranslateTransform X="0" Y="0"/> 
             </TransformGroup> 
            </ContentPresenter.LayoutTransform> 
           </ContentPresenter> 
          </DockPanel> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsExpanded" Value="True"> 
           <Trigger.EnterActions> 
            <BeginStoryboard Storyboard="{StaticResource Timeline1}"/> 
           </Trigger.EnterActions> 
           <Trigger.ExitActions> 
            <BeginStoryboard Storyboard="{StaticResource Timeline2}"/> 
           </Trigger.ExitActions> 
          </Trigger> 
          <Trigger Property="ExpandDirection" Value="Up"> 
           <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Top"/> 
           <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Bottom"/> 
          </Trigger> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
          </Trigger> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter Property="Visibility" Value="Visible" TargetName="ExpandSite"/> 
          </Trigger> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter Property="Visibility" Value="Visible" TargetName="ExpandSite"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Page.Resources> 
    <Grid> 
     <Expander HorizontalAlignment="Left" VerticalAlignment="Top" Background="Red" BorderBrush="Green" Width="100" Header="" x:Name="expander" Style="{DynamicResource ExpanderStyle1}" IsExpanded="True"> 
      <ListBox IsSynchronizedWithCurrentItem="True" x:Name="listBox"> 
       <ListBoxItem Content="ListBoxItem"/> 
       <ListBoxItem Content="ListBoxItem"/> 
       <ListBoxItem Content="ListBoxItem"/> 
      </ListBox> 
     </Expander> 
    </Grid> 
</Page> 

回答

0

使用样式触发器。在这里你去:

<Expander> 
    <Expander.Style> 
     <Style> 
      <Setter Property="Background" Value="Green" /> 
      <Style.Triggers> 
       <Trigger Property="Expander.IsMouseOver" Value="True"> 
        <Setter Property="Expander.IsExpanded" Value="True" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Expander.Style> 
    <Rectangle Width="100" Height="100" Fill="Red" Stroke="Black" /> 
</Expander> 
+0

我可能会遗漏一些东西,但是如果我尝试将其添加到模板中,则不起作用。如果我添加到扩展器本身,我会得到一个“样式属性已经设置在扩展器上”。 – Ian

+0

明确地把它放在扩展器上。如果你得到“已经设置”,那么它意味着你可能有这个冲突。只需将样式业务移至