VisualStates:第一次国家变革没有过渡

时间:2012-05-25 14:33:37

标签: silverlight transition visualstatemanager

在我的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>

1 个答案:

答案 0 :(得分:1)

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