在WPF中的Window.Resources中声明时,EventTrigger不起作用
发布时间:2020-12-14 04:38:14 所属栏目:Windows 来源:网络整理
导读:我是 WPF的新手,所以我可能会遗漏一些必要的东西,但我已经尝试并试图找出以下现象的解释,但无济于事. 基本上,以下代码工作(显示动画): Window.Resources Storyboard x:Key="LoadStoryBoard"AutoReverse="True"RepeatBehavior="Forever" DoubleAnimationUsin
我是
WPF的新手,所以我可能会遗漏一些必要的东西,但我已经尝试并试图找出以下现象的解释,但无济于事.
基本上,以下代码工作(显示动画): <Window.Resources> <Storyboard x:Key="LoadStoryBoard" AutoReverse="True" RepeatBehavior="Forever"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="button1" Storyboard.TargetProperty="(Button.Opacity)"> <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0.4" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </Window.Resources> ... <Button x:Name="button1" Grid.Column="0" Grid.Row="1" Style="{StaticResource Load}"> <Button.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard Storyboard="{StaticResource LoadStoryBoard}" /> </EventTrigger> </Button.Triggers> </Button> 但是,当我尝试将eventrigger置于下面的Load Style中时,动画将不再出现: <Window.Resources> <Storyboard x:Key="LoadStoryBoard" AutoReverse="True" RepeatBehavior="Forever"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="button1" Storyboard.TargetProperty="(Button.Opacity)"> <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0.4" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </Window.Resources> ... <Style x:Key="Load" TargetType="Button"> ... <Style.Triggers> ... <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard Storyboard="{StaticResource LoadStoryBoard}" /> </EventTrigger> </Style.Triggers> </Style> 解决方法
在触发器的样式中不能使用具有TargetName的对象,这样的动画.为此,将它们放在触发器模板< ControlTemplate.Triggers>中.引自
link:
以下作品: <Window.Resources> <Storyboard x:Key="LoadStoryBoard" AutoReverse="True" RepeatBehavior="Forever"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="button1" Storyboard.TargetProperty="(Button.Opacity)"> <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0.4" /> </DoubleAnimationUsingKeyFrames> </Storyboard> <Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Background" Value="Green" /> <Setter Property="Foreground" Value="White" /> <Setter Property="FontSize" Value="14" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="button1" CornerRadius="0" Background="{TemplateBinding Background}"> <Grid> <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0" /> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouSEOver" Value="True"> <Setter Property="Background" Value="Orange" /> </Trigger> <EventTrigger RoutedEvent="Button.Loaded"> <BeginStoryboard Storyboard="{StaticResource LoadStoryBoard}" /> </EventTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button Name="TestButton" Style="{StaticResource ButtonStyle}" Width="100" Height="30" Content="Test" Grid.Column="0" Grid.Row="1" /> </Grid> 请注意,现在Border中指定的模板中的TargetName:< Border x:Name =“button1”... />. 注意:或者,您可以删除Storyboard.TargetName,因为它不支持触发样式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- wpf – 如何在XAML中绘制单独的圆圈?
- windows – 批处理文件中的国际字符
- Win32 DrawText行高
- 通过Microsoft.AspNetCore.App元包简化程序集的引用
- windows-8 – Windows 8上的Windows Mobile / Embedded Dev
- 什么是不同的Team Foundation Server(TFS)和Microsoft Visu
- windows – 如何为SQL Server创建最小权限用户?
- Windows应用程序池在Windows 10中以32位模式失败
- Windows下tomcat进程监控批处理程序
- 在基于Windows的VPS中从Java访问Ubuntu服务器中托管的MS Ac