在名称范围的样式中找不到故事板

Phi*_*hil 5 wpf xaml triggers styles storyboard

我遇到的问题是,如果我将样式基于包含故事板的第二个样式,那么Trigger.ExitAction中会有一个例外.

  • 如果我将鼠标悬停在下面演示中的任何一个矩形上,那么故事板将会运行并且矩形会改变颜色.
  • 当鼠标离开带有style ='rectStyle'的红色矩形时,故事板将被删除.
  • 当鼠标离开蓝色矩形(使用派生样式)时,我得到以下异常:

    InvalidOperationException:在'System.Windows.Style'的名称范围内找不到'MouseOverStoryboard'名称.

所以:

  • 以基本风格制作故事板是否有效?
  • 是否有更明确的方式引用BeginStoryboardName以便不会发生此错误?
  • 还有其他建议吗?

最后,我正在尝试重新使用包含其他几种风格的触发器和故事板的样式.

这是一些演示问题的简单代码:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.Resources>
        <Style x:Key="rectStyle" TargetType="{x:Type Rectangle}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard x:Name="MouseOverStoryboard">
                            <Storyboard>
                                <ColorAnimation To="PaleGoldenrod" 
                                   Storyboard.TargetProperty="(Fill).(Color)" 
                                   Duration="0:0:1"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="MouseOverStoryboard"/>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="rectStyle2" TargetType="{x:Type Rectangle}" 
               BasedOn="{StaticResource rectStyle}"/>
    </Grid.Resources>

    <Rectangle Grid.Row="0" Width="100" Height="100" Fill="Red" 
               Style="{StaticResource rectStyle}" />
    <Rectangle Grid.Row="1" Width="100" Height="100" Fill="Blue" 
               Style="{StaticResource rectStyle2}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

ero*_*ald 7

如果要包含,最好的办法是避免派生样式StopStoryboard.实际上有2个样式的实例,并且派生样式的范围是错误的.我相信这是设计的.您可以将该故事板抽象到资源中的外部作用域,并在两种样式中复制触发器,引用Storyboard StaticResource.它可能无法解决您的问题,因为您可能对派生样式有特定要求,但我不认为您有一个选择.

因为如果你想使用StopStoryboard你根本不能继承基本风格,你必须做这样的事情:

<Grid>
<Grid.RowDefinitions>
  <RowDefinition/>
  <RowDefinition/>
</Grid.RowDefinitions>
<Grid.Resources>
  <Storyboard x:Key="mouseOver">
    <ColorAnimation
      AutoReverse="True"
      Duration="0:0:1"
      RepeatBehavior="Forever"
      Storyboard.TargetProperty="(Fill).(Color)"
      To="PaleGoldenrod"/>
  </Storyboard>
  <Style x:Key="rectStyle" TargetType="{x:Type Rectangle}">
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Trigger.EnterActions>
          <BeginStoryboard x:Name="MouseOverStoryboard" Storyboard="{StaticResource mouseOver}"/>
        </Trigger.EnterActions>
        <Trigger.ExitActions>
          <StopStoryboard BeginStoryboardName="MouseOverStoryboard"/>
        </Trigger.ExitActions>
      </Trigger>
    </Style.Triggers>
  </Style>
  <Style x:Key="rectStyle2" TargetType="{x:Type Rectangle}">
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Trigger.EnterActions>
          <BeginStoryboard x:Name="MouseOverStoryboard1" Storyboard="{StaticResource mouseOver}"/>
        </Trigger.EnterActions>
        <Trigger.ExitActions>
          <StopStoryboard BeginStoryboardName="MouseOverStoryboard1"/>
        </Trigger.ExitActions>
      </Trigger>
    </Style.Triggers>
  </Style>
</Grid.Resources>
<Rectangle
  Width="100"
  Height="100"
  Grid.Row="0"
  Fill="Red"
  Style="{StaticResource rectStyle}"/>
<Rectangle
  Width="100"
  Height="100"
  Grid.Row="1"
  Fill="Blue"
  Style="{StaticResource rectStyle2}"/>
Run Code Online (Sandbox Code Playgroud)