我正在尝试使用XAML在WPF中设置ScrollViewer外观,但遇到了挑战。通常,我的ScrollBar是隐藏的,但是当用户开始平移屏幕时,它们将变为可见。我已经使用ScrollChanged实现了此功能,如下所示:
<!-- Storyboards -->
<Storyboard x:Key="uiStoryboardFader">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.0" />
<EasingDoubleKeyFrame KeyTime="0:0:2.0" Value="1.0" />
<EasingDoubleKeyFrame KeyTime="0:0:2.3" Value="0.0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<!-- Style Declaration -->
<!-- Control Template -->
<ScrollBar x:Name="PART_VerticalScrollBar" Opacity="0.0" />
<ScrollBar x:Name="PART_HorizontalScrollBar" Opacity="0.0" />
<!-- Control Template Triggers -->
<EventTrigger RoutedEvent="ScrollViewer.ScrollChanged">
<BeginStoryboard Storyboard="{StaticResource uiStoryboardFader}"
Storyboard.TargetName="PART_VerticalScrollBar" />
<BeginStoryboard Storyboard="{StaticResource uiStoryboardFader}"
Storyboard.TargetName="PART_HorizontalScrollBar" />
</EventTrigger>
<!-- Closing -->
即使我将TargetName设置为“ PART_VerticalScrollBar”和“ PART_HorizontalScrollBar”,此操作仍会更改整个ScrollViewer的不透明度。但是,当我在情节提要声明中手动设置TargetName时,一切正常。无论如何,我可以通过在BeginStoryboard中分配TargetName来完成这项工作吗?
最佳答案
我认为您只是不想复制粘贴动画?您尝试执行的操作可能永远无法解决,该属性的值不会被继承,并且您仍然需要两个情节提要,您不能两次启动同一情节提要(更不用说在不同控件上了)。
这样至少可以保留一个动画实例:
<DoubleAnimationUsingKeyFrames x:Key="Animation"
Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.0" />
<EasingDoubleKeyFrame KeyTime="0:0:2.0" Value="1.0" />
<EasingDoubleKeyFrame KeyTime="0:0:2.3" Value="0.0" />
</DoubleAnimationUsingKeyFrames>
<!-- ... -->
<EventTrigger RoutedEvent="ScrollViewer.ScrollChanged">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="PART_VerticalScrollBar">
<StaticResourceExtension ResourceKey="Animation" />
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="PART_HorizontalScrollBar">
<StaticResourceExtension ResourceKey="Animation" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
关于c# - 在EventTrigger中为Storyboard分配TargetName,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8209104/