2010-10-15 75 views
2

我正在使用studio 2010和silverlight 4構建自定義控件。 我正在嘗試使用可視狀態管理器。VisualStateManager不做任何事(silverlight)

下列與XML:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="clr-namespace:SilverView"> 
    <Style TargetType="controls:ScaleImage"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="controls:ScaleImage"> 
        <Grid> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualStateGroup.Transitions> 
            <VisualTransition To="MouseOver" 
                 GeneratedDuration="0:0:.5"/> 
            <VisualTransition To="Normal" 
                 GeneratedDuration="0:0:.5"/> 
           </VisualStateGroup.Transitions> 
           <VisualState x:Name="Normal"> 
            <Storyboard> 
             <DoubleAnimation 
             Storyboard.TargetName="img" 
             Storyboard.TargetProperty="Width" 
             From="50" To="100"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <DoubleAnimation 
             Storyboard.TargetName="img" 
             Storyboard.TargetProperty="Width" 
             From="50" To="100"/> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Image Name="img" Width="50"> 
          <Image.RenderTransform> 
           <ScaleTransform x:Name="scale"/> 
          </Image.RenderTransform> 
         </Image> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

沒有任何反應,當我將鼠標放在圖片。 鼠標懸停時如何放大圖像?

感謝

回答

4

VisualStateManager.VisualStateGroups附屬屬性定義了一組可視狀態,但組的名稱和狀態的名稱只是名稱,它們實際上並未啓用它們自動描述的功能。

這取決於您的控件中的代碼,以確定它何時處於特定狀態,然後通知VisualStateManager該選項。你做這樣的代碼: -

VisualStateManager.GotoState(this, "MouseOver", true); 

通常你會收集像鼠標是否在通過各種控制事件的控制信息,並有一箇中央UpdateVisualState功能,設置所有適當的狀態。

+0

當存在等價行爲時,您不應該鼓勵代碼隱藏:) – 2010-10-15 13:26:31

2

在XAML以上你只定義狀態組,並用類似「鼠標懸停」名稱的狀態。你實際上並沒有引起國家的變化,因爲它們顯然沒有與任何事件相關聯。

如果您還不是,請嘗試使用GoToState行爲來觸發您的控件的狀態更改。

您是否有更多的代碼或XML觸發狀態更改?