在Style.Triggers中使用Storyboard异常而不是FrameworkElement.Triggers

时间:2016-03-01 15:33:24

标签: wpf xaml

我有一个自定义按钮,用于打开/关闭DevExpress FlyoutControl。以下代码具有在FrameworkElement.Triggers集合中定义的按钮单击事件触发器:

<controls:GlyphButton Name="AvailableEcusGlyphButton">
    <controls:GlyphButton.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
             <BeginStoryboard>
                 <Storyboard>
                     <BooleanAnimationUsingKeyFrames Storyboard.Target="{x:Reference AvailableEcusFlyout}"
                                                     Storyboard.TargetProperty="IsOpen">
                           <DiscreteBooleanKeyFrame KeyTime="0:0:0"
                                                    Value="{Binding ElementName=AvailableEcusFlyout, Path=IsOpen, Converter={StaticResource BooleanInverterConverter}}" />
                     </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
       </EventTrigger>
   </controls:GlyphButton.Triggers>
</controls:GlyphButton>

但是,在阅读了Element.Triggers不常用的建议之后,我决定以应用于按钮的样式移动故事板。

<controls:GlyphButton Name="AvailableEcusGlyphButton">
    <controls:GlyphButton.Style>
        <Style TargetType="{x:Type controls:GlyphButton}">
             <Style.Triggers>
                  <EventTrigger RoutedEvent="Button.Click">
                      <BeginStoryboard>
                          <Storyboard>
                              <BooleanAnimationUsingKeyFrames Storyboard.Target="{x:Reference AvailableEcusFlyout}"
                                                       Storyboard.TargetProperty="IsOpen">
                                  <DiscreteBooleanKeyFrame KeyTime="0:0:0"
                                                    Value="{Binding ElementName=AvailableEcusFlyout, Path=IsOpen, Converter={StaticResource BooleanInverterConverter}}" />
                              </BooleanAnimationUsingKeyFrames>
                          </Storyboard>
                      </BeginStoryboard>
                 </EventTrigger>
              </Style.Triggers>
           </Style>
        </controls.GlyphButton>

加载上述代码后,triggers collection会在运行时抛出以下异常。

{"After a 'TriggerCollection' is in use (sealed), it cannot be modified."}

是什么给出的?我没有看到在这里修改触发器集合的内容。

1 个答案:

答案 0 :(得分:1)

使用您的第一种方法,

并完全删除BooleanAnimationUsingKeyFrames内容并使用ObjectAnimationUsingKeyFrames代替,如下所示:

<Storyborad>
    <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference AvailableEcusFlyout}"
                                Storyboard.TargetProperty="IsOpen">
        <DiscreteObjectKeyFrame KeyTime="0:0:0"
                            Value="{Binding ElementName=AvailableEcusFlyout, Path=IsOpen, Converter={StaticResource BooleanInverterConverter}}" />
    </ObjectAnimationUsingKeyFrames>
</Storyboard>