WPF - XAML - 从父样式调用嵌套样式的动画

时间:2016-08-05 08:18:45

标签: wpf xaml animation storyboard visualbrush

我试图修改ItemsControl以获得水印来指示内容的行为。

我在VisualBrush中使用Label来显示单词" OR"作为ItemsControl的背景(控件将包含属性过滤器,它们将是“或”在一起)。背景的变化由ItemsControl上的IsMouseOver触发。

问题是这样的:如果我直接在Label中设置不透明度,我可以让我的VisualBrush(参见xaml)出现/消失但是如果我尝试使用嵌套样式作为标签,我就无法获得Opacity动画。我已经尝试了几种没有成功的方法,所以任何关于我的错误的指针都会感激不尽。

我已经尝试了两个动画(一个已注释掉)。我还尝试使用ColorAnimation在Label上设置Foreground,但没有成功。

非常感谢 伊恩卡森

<ItemsControl x:Name="OrFilterItemsTarget"
              ItemsSource="{Binding Assets.OrFilters}"
              ItemTemplateSelector="{StaticResource FilterTargetTemplateSelector}"
              ItemContainerStyle="{StaticResource DraggableItemStyle}"
              BorderThickness="0,0,0,0.5"
              BorderBrush="DimGray"
              AllowDrop="True"
              IsHitTestVisible="True"
              Margin="0,2.95,15.934,77"
              HorizontalAlignment="Right"
              Width="105">
    <ItemsControl.Style>
        <Style TargetType="{x:Type ItemsControl}">
            <Style.Resources>
                <Style x:Key="LabelStyle"
                       TargetType="{x:Type Label}">
                    <Style.Resources>
                        <Storyboard x:Key="FadeUp">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                          Storyboard.TargetProperty="Opacity"
                                                          FillBehavior="HoldEnd">
                                <SplineDoubleKeyFrame KeyTime="00:00:2"
                                                     Value="0.5" />
                            </DoubleAnimationUsingKeyFrames>
                            <!--<DoubleAnimation Storyboard.TargetProperty="Opacity"
                                             From="0" To="0.5"
                                             Duration="0:0:2" />-->
                        </Storyboard>
                        <Storyboard x:Key="FadeDown">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetProperty="Opacity"
                                                           FillBehavior="Stop">
                                <SplineDoubleKeyFrame KeyTime="00:00:2"
                                                      Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <!--<DoubleAnimation Storyboard.TargetProperty="Opacity"
                                             From="0.5" To="0"
                                             Duration="0:0:2" />-->
                        </Storyboard>
                    </Style.Resources>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=OrFilterItemsTarget}"
                                     Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource FadeUp}" />
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                        <DataTrigger  Binding="{Binding Path=IsMouseOver, ElementName=OrFilterItemsTarget}"
                                      Value="False">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource FadeDown}" />
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
                <VisualBrush x:Key="FilterContentType"
                             AlignmentX="Center"
                             AlignmentY="Center"
                             Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="OR"
                               Foreground="DarkGray"
                               Style="{StaticResource LabelStyle}">
                        </Label>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="IsMouseOver"
                         Value="True">
                    <Setter Property="Background"
                            Value="{StaticResource FilterContentType}" />
                </Trigger>
                <Trigger Property="IsMouseOver"
                         Value="False">
                    <Setter Property="Background"
                            Value="Transparent" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.Style>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"
                       Background="Transparent">
                <i:Interaction.Behaviors>
                    <ei:FluidMoveBehavior AppliesTo="Children"
                                          Duration="0:0:0.3">
                        <ei:FluidMoveBehavior.EaseY>
                            <BackEase EasingMode="EaseIn"
                                      Amplitude="0.1" />
                        </ei:FluidMoveBehavior.EaseY>
                    </ei:FluidMoveBehavior>
                </i:Interaction.Behaviors>
            </WrapPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

1 个答案:

答案 0 :(得分:0)

最后我发现自己过度思考了这个问题。嵌套样式是不必要的。解决方案是将后台设置为在ItemsControl中自己的标记内的VisualBrush(其内容设置为所需的最终外观),然后直接在ItemsControl上使用EventTriggers设置VisualBrush的不透明度。请注意管理鼠标和拖动用户活动的各种事件。

感谢所有想到这个问题的人。最终的XAML看起来像这样,希望对某人有用。

 <ItemsControl x:Name="OrFilterItemsTarget"
               ItemsSource="{Binding Assets.OrFilters}"
               ItemTemplateSelector="{StaticResource FilterTargetTemplateSelector}"
               ItemContainerStyle="{StaticResource DraggableItemStyle}"
               BorderThickness="0,0,0,0.5"
               BorderBrush="DimGray"
               AllowDrop="True"
               IsHitTestVisible="True"
               Margin="0,2.95,15.934,77"
               HorizontalAlignment="Right"
               Width="105">
     <ItemsControl.Background>
         <VisualBrush x:Name="OrFilterContentType"
                      AlignmentX="Center"
                      AlignmentY="Center"
                      Stretch="None"
                      Opacity="0">
             <VisualBrush.Visual>
                 <Label Content="OR"
                        Foreground="DarkGray"
                        Opacity="0.5" />
             </VisualBrush.Visual>
         </VisualBrush>
     </ItemsControl.Background>
     <ItemsControl.Triggers>
         <EventTrigger RoutedEvent="ItemsControl.MouseEnter">
             <EventTrigger.Actions>
                 <BeginStoryboard>
                     <Storyboard>
                         <DoubleAnimation Storyboard.TargetName="OrFilterContentType"
                                          Storyboard.TargetProperty="(Brush.Opacity)"
                                          From="0" To="1"
                                          Duration="0:0:0.7" />
                     </Storyboard>
                 </BeginStoryboard>
             </EventTrigger.Actions>
         </EventTrigger>
         <EventTrigger RoutedEvent="ItemsControl.DragEnter">
             <EventTrigger.Actions>
                 <BeginStoryboard>
                     <Storyboard>
                         <DoubleAnimation Storyboard.TargetName="OrFilterContentType"
                                          Storyboard.TargetProperty="(Brush.Opacity)"
                                          From="0"
                                          To="1"
                                          Duration="0:0:0.7" />
                     </Storyboard>
                 </BeginStoryboard>
             </EventTrigger.Actions>
         </EventTrigger>
         <EventTrigger RoutedEvent="ItemsControl.MouseLeave">
             <EventTrigger.Actions>
                 <BeginStoryboard>
                     <Storyboard>
                         <DoubleAnimation Storyboard.TargetName="OrFilterContentType"
                                          Storyboard.TargetProperty="(Brush.Opacity)"
                                          From="1" To="0"
                                          Duration="0:0:0.7" />
                     </Storyboard>
                 </BeginStoryboard>
             </EventTrigger.Actions>
         </EventTrigger>
         <EventTrigger RoutedEvent="ItemsControl.DragLeave">
             <EventTrigger.Actions>
                 <BeginStoryboard>
                     <Storyboard>
                         <DoubleAnimation Storyboard.TargetName="OrFilterContentType"
                                          Storyboard.TargetProperty="(Brush.Opacity)"
                                          From="1"
                                          To="0"
                                          Duration="0:0:0.7" />
                     </Storyboard>
                 </BeginStoryboard>
             </EventTrigger.Actions>
         </EventTrigger>
         <EventTrigger RoutedEvent="ItemsControl.Drop">
             <EventTrigger.Actions>
                 <BeginStoryboard>
                     <Storyboard>
                         <DoubleAnimation Storyboard.TargetName="OrFilterContentType"
                                          Storyboard.TargetProperty="(Brush.Opacity)"
                                          From="1"
                                          To="0"
                                          Duration="0:0:0.7" />
                     </Storyboard>
                 </BeginStoryboard>
             </EventTrigger.Actions>
         </EventTrigger>
     </ItemsControl.Triggers>
     <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <WrapPanel Orientation="Horizontal"
                        Background="Transparent">
                 <i:Interaction.Behaviors>
                     <ei:FluidMoveBehavior AppliesTo="Children"
                                           Duration="0:0:0.3">
                         <ei:FluidMoveBehavior.EaseY>
                             <BackEase EasingMode="EaseIn"
                                       Amplitude="0.1" />
                         </ei:FluidMoveBehavior.EaseY>
                     </ei:FluidMoveBehavior>
                 </i:Interaction.Behaviors>
             </WrapPanel>
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
 </ItemsControl>