0

在我的 Silverlight 4 应用程序中,我创建了一个自定义控件并添加了 Visual States incl。过渡。第一次执行状态更改时,没有转换,只有从一个状态到另一个状态的快速变化(就像根本没有转换一样)。所有以下状态更改都按预期执行转换。我试图在构造函数中设置初始状态,但这没有帮助。有谁知道,为什么第一次没有过渡?

这是我的自定义控件中的一些代码:

[TemplateVisualState(GroupName="CommonState", Name="Expand")]
[TemplateVisualState(GroupName="CommonState", Name="Collapse")]
public class CollapsibleContainer : ContentControl
{
  public CollapsibleContainer()
  {
    this.DefaultStyleKey = typeof(CollapsibleContainer);
    VisualStateManager.GoToState(this, "Expand", false);
  }

  private void BorderHeader_Click(object sender, MouseButtonEventArgs args)
  {
    if (_contentPresenter.Visibility == Visibility.Collapsed)
    {
      _contentPresenter.Visibility = Visibility.Visible;
      VisualStateManager.GoToState(this, "Expand", true);
    }
    else
    {
      _contentPresenter.Visibility = Visibility.Collapsed;
      VisualStateManager.GoToState(this, "Collapse", true);
    }
  }
}

这是视觉状态的定义:

<VisualStateManager.VisualStateGroups>
  <VisualStateGroup x:Name="CommonState">
    <VisualState x:Name="Expand" />
    <VisualState x:Name="Collapse">
      <Storyboard>
        <DoubleAnimation Duration="0" To="180" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="ImageArrowNormal" d:IsOptimized="True"/>                                        
        <DoubleAnimation Duration="0" To="180" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="ImageArrowHover" d:IsOptimized="True"/>
      </Storyboard>
    </VisualState>

    <VisualStateGroup.Transitions>
      <VisualTransition x:Name="Expand2Collapse" From="Expand" To="Collapse">
        <Storyboard>
          <DoubleAnimation Duration="0:0:0.3" To="180" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="ImageArrowNormal"/>
          <DoubleAnimation Duration="0:0:0.3" To="180" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="ImageArrowHover"/>
        </Storyboard>
      </VisualTransition>

      <VisualTransition x:Name="Collapse2Expand" From="Collapse" To="Expand">
        <Storyboard>
          <DoubleAnimation Duration="0:0:0.3" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="ImageArrowNormal"/>
          <DoubleAnimation Duration="0:0:0.3" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="ImageArrowHover"/>
        </Storyboard>
      </VisualTransition>
    </VisualStateGroup.Transitions>
  </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
4

1 回答 1

1

尝试在 ApplyTemplate 中设置初始状态,而不是在构造函数中。

于 2012-05-25T15:13:19.760 回答