访问按钮样式的故事板
我的问题是我有一个 Button
并且我想访问 Storyboard
,它是指定样式的一部分。
<Button x:Name="pictureFolderButton" Content="Pictures" Style="{StaticResource ImageTileButtonStyle}" Click="pictureFolderButton_Click" />
该样式非常全面,因此我将仅发布其中的一部分:
<Style x:Key="ImageTileButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="OnLoaded1"/>
</ControlTemplate.Resources>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
...
</VisualStateGroup>
<VisualStateGroup x:Name="AnimationStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:1">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseOut"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="ExpandedFull">
<Storyboard x:Name="expandStoryBoard" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="border1">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="130"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="130"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="47"/>
<EasingDoubleKeyFrame KeyTime="0:0:8" Value="47"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter RecognizesAccessKey="True" VerticalAlignment="Stretch" Margin="0,47,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我只想在“ExpandedFull”动画结束时收到通知。因此,我认为我必须以编程方式获取“expandStoryBoard”并添加一个 Completed 事件处理程序。
我唯一要做的就是在运行时访问按钮的样式:
Style style = pictureFolderButton.FindResource("ImageTileButtonStyle") as Style;
我该如何继续? 非常感谢!
My problem is that I have a Button
and I want to access the Storyboard
, which is a part of the assigned style.
<Button x:Name="pictureFolderButton" Content="Pictures" Style="{StaticResource ImageTileButtonStyle}" Click="pictureFolderButton_Click" />
The style is very comprehensive so I'll post only a part of it:
<Style x:Key="ImageTileButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="OnLoaded1"/>
</ControlTemplate.Resources>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
...
</VisualStateGroup>
<VisualStateGroup x:Name="AnimationStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:1">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseOut"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="ExpandedFull">
<Storyboard x:Name="expandStoryBoard" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="border1">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="130"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="130"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="47"/>
<EasingDoubleKeyFrame KeyTime="0:0:8" Value="47"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter RecognizesAccessKey="True" VerticalAlignment="Stretch" Margin="0,47,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I just want to get notified when the "ExpandedFull" animation has ended. Therefore, I thought I have to get the "expandStoryBoard" programmatically and add a Completed event handler.
The only thing I got working is to access the button's style at runtime:
Style style = pictureFolderButton.FindResource("ImageTileButtonStyle") as Style;
How do I have to proceed?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理论上,您应该能够沿着按钮的视觉和逻辑树向下移动到情节提要,但如果您将模板中的 Grid 命名为“grid”,则这是相当乏味的,例如以下可能有效:
想到的另一种方法是将故事板提取到资源中,但我不确定这是否会产生任何副作用,即:
那么您应该能够使用
FindResource
按钮以获取情节提要。希望其中一些能起作用,或者至少有一点帮助。
In theory you should be able to go down the visual and logical tree of your button to get to the storyboard, but that is rather tedious, if you name the
Grid
in the template "grid", something like the following might work:Another way that comes to mind is extracting your storyboard to a resource, but i am not sure if this will have any side effects, i.e.:
Then you should be able to use
FindResource
on the button to get the storyboard.Hopefully some of that works or at least helps a bit.
只需尝试使用
StoryBoard
名称“OnLoaded1”即可:Just try with this with
StoryBoard
name "OnLoaded1":如果将
Storyboard
添加到资源中,则可以在XAML文件中设置Timeline.Completed
事件的事件处理程序,并在相应的类中实现该处理程序。在控件的资源部分定义
Storyboard
,如下所示:将
Storyboard
引用为静态资源:在相应的资源中实现
Completed
事件处理程序班级:If you add the
Storyboard
to the resources, you can set the event handler for theTimeline.Completed
event in the XAML file and implement the handler in the corresponding class.Define the
Storyboard
in the Resources section of your control like this:Reference the
Storyboard
as a static resource:Implement the
Completed
event handler in the corresponding class: