2010-12-17 132 views
0

这是一个难倒我。所以我有下面的示例应用程序,当鼠标悬停在它上面时,它应该为边界元素的不透明度设置动画。 `VisualStateManager.GoToState总是返回false

<UserControl.Resources> 
    <Style x:Key="borderstyle" TargetType="ContentControl"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ContentControl"> 
        <Grid> 

         <VisualStateManager.VisualStateGroups> 

          <VisualStateGroup> 
           <VisualState x:Name="FirstState"> 
            <Storyboard> 
             <DoubleAnimation To="1.0" Storyboard.TargetName="border" Storyboard.TargetProperty="Opacity" FillBehavior="HoldEnd"/> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 

         </VisualStateManager.VisualStateGroups> 

         <Border Background="Blue" x:Name="border" Opacity="0.0"/> 

        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 

    </Style> 
</UserControl.Resources> 


<Grid x:Name="LayoutRoot" Background="White"> 
    <Border BorderBrush="Red" BorderThickness="1" Width="51" Height="51"> 
     <ContentControl Width="50" Height="50" Style="{StaticResource borderstyle}" MouseEnter="OnMouseEntered" /> 
    </Border> 
</Grid> 

`

而在后面的代码我有以下代码...

private void OnMouseEntered(object sender, MouseEventArgs e) 
    { 
     bool status = VisualStateManager.GoToState(this, "FirstState", true); 
    } 

而且......没有任何反应。 状态始终是错误的,并且动画永远不会触发。

我不知道这里缺少什么。

回答

3

其状态正在改变,则应指定您的ContentControl作为控制:

private void OnMouseEntered(object sender, MouseEventArgs e) 
{ 
    bool status = VisualStateManager.GoToState((ContentControl)sender, "FirstState", false); 
} 
+0

哇...很酷的感谢。完美的作品。我认为我很难过,因为我通常建立自定义控件,并通过“这个”工作得很好。现在就行了:)谢谢 – Senkwe 2010-12-17 17:56:48

1

添加ApplyTemplate()到您的控制(在需要时控制时动态地(编程)添加

1

使用VisualStateManager状态转换。任何时候在页面加载事件处理程序中或之后