2010-12-17 131 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狀態轉換。任何時候在頁面加載事件處理程序中或之後