WPF中Window.Resources中声明的EventTrigger不起作用 [英] EventTrigger Not Working When Declared in Window.Resources in WPF

查看:152
本文介绍了WPF中Window.Resources中声明的EventTrigger不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手,所以我可能会错过一些必要的东西,但是我已经尝试了一些解决以下现象的解决方案。
基本上,以下代码工作(显示动画):

 < Window.Resources> 
< Storyboard x:Key =LoadStoryBoard
AutoReverse =True
RepeatBehavior =Forever>
< DoubleAnimationUsingKeyFrames Storyboard.TargetName =button1
Storyboard.TargetProperty =(Button.Opacity)>
< EasingDoubleKeyFrame KeyTime =0:0:0.7Value =0.4/>
< / DoubleAnimationUsingKeyFrames>
< / Storyboard>
< /Window.Resources>
...
< Button x:Name =button1Grid.Column =0Grid.Row =1Style ={StaticResource Load}>
< Button.Triggers>
< EventTrigger RoutedEvent =已加载>
< 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.7Value =0.4/>
< / DoubleAnimationUsingKeyFrames>
< / Storyboard>
< /Window.Resources>
...
< Style x:Key =LoadTargetType =Button>
...
< Style.Triggers>
...
< EventTrigger RoutedEvent =加载>
< BeginStoryboard Storyboard ={StaticResource LoadStoryBoard}/>
< / EventTrigger>
< /Style.Triggers>
< / Style>


解决方案

Style 触发器不能使用 TargetName 的对象,这样的动画。为此,它们被放置在触发器模板< ControlTemplate.Triggers> 中。引用自链接


TargetName不适用于触发器收藏一种风格。一种风格没有名称范围,因此在名称中引用元素是没有意义的。但是一个模板(DataTemplate或ControlTemplate)确实有一个命名空间。


以下工作原理:

 < Window.Resources> 
< Storyboard x:Key =LoadStoryBoardAutoReverse =TrueRepeatBehavior =Forever>
< DoubleAnimationUsingKeyFrames Storyboard.TargetName =button1Storyboard.TargetProperty =(Button.Opacity)>
< EasingDoubleKeyFrame KeyTime =0:0:0.7Value =0.4/>
< / DoubleAnimationUsingKeyFrames>
< / Storyboard>

< Style x:Key =ButtonStyleTargetType ={x:Type Button}>
< Setter Property =BackgroundValue =Green/>
< Setter Property =ForegroundValue =White/>
< Setter Property =FontSizeValue =14/>
< Setter Property =FocusVisualStyleValue ={x:Null}/>
< Setter Property =SnapsToDevicePixelsValue =True/>

< Setter属性=模板>
< Setter.Value>
< ControlTemplate TargetType ={x:Type Button}>
< Border x:Name =button1CornerRadius =0Background ={TemplateBinding Background}>
< Grid>
< ContentPresenter x:Name =MyContentPresenterContent ={TemplateBinding Content}Horizo​​ntalAlignment =CenterVerticalAlignment =CenterMargin =0,0,0,0/>
< / Grid>
< / Border>

< ControlTemplate.Triggers>
< Trigger Property =IsMouseOverValue =True>
< Setter Property =BackgroundValue =Orange/>
< / Trigger>

< EventTrigger RoutedEvent =Button.Loaded>
< BeginStoryboard Storyboard ={StaticResource LoadStoryBoard}/>
< / EventTrigger>
< /ControlTemplate.Triggers>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /Window.Resources>

< Grid>
< Button Name =TestButtonStyle ={StaticResource ButtonStyle}Width =100Height =30Content =TestGrid.Column =0Grid.Row =1/ >
< / Grid>注意现在在指定的模板中 TargetName

边框中:< Border x:Name =button1... />



注意: 或者,您可以删除 Storyboard.TargetName ,因为它不支持触发风格。


I am new to WPF, so I may be missing something essential, but I have experimented and tried to come up with an explanation for the following phenomenon, to no avail. Basically, the following code works (displays animation):

    <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>

However, when I try to put the eventrigger in the Load Style in the following, the animation ceases to appear:

    <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>

解决方案

In the Style of triggers can not use objects with TargetName, such animation. To do this, they are placed in triggers template <ControlTemplate.Triggers>. Quote from link:

TargetName is not intended for use within the Triggers collection of a Style. A style does not have a namescope, so it does not make sense to refer to elements by name there. But a template (either DataTemplate or ControlTemplate) does have a namescope.

The following works:

<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,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>

Notice that now TargetName in the template specified in the Border: <Border x:Name="button1" .../>.

Note: Or, you can just remove the Storyboard.TargetName, since it triggers the style is not supported.

这篇关于WPF中Window.Resources中声明的EventTrigger不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆