2010-09-11 58 views
1

我的問題很簡單:它甚至有可能嗎?動畫從相互排斥的VisualStateGroups動畫相同的屬性

假設我想要設置ListBoxItem的樣式,使其默認情況下具有黑色前景,選擇時爲藍色,鼠標移過時爲紅色。我結束了這樣的事情:

<!-- assume the default foreground color is black --> 
<ControlTemplate TargetType="ListBoxItem"> 
    <Grid Background="{TemplateBinding Background}"> 
     <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="CommonStates"> 
       <VisualState x:Name="Normal"/> 

       <VisualState x:Name="MouseOver"> 
        <Storyboard> 
         <ColorAnimation Duration="0:0:0.2" To="Red" Storyboard.TargetName="contentControl" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"/> 
        </Storyboard> 
       </VisualState> 
      </VisualStateGroup> 

      <VisualStateGroup x:Name="SelectionStates"> 
       <VisualState x:Name="Unselected"/> 

       <VisualState x:Name="Selected"> 
        <Storyboard> 
         <ColorAnimation Duration="0:0:0.2" To="Blue" Storyboard.TargetName="contentControl" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"/> 
        </Storyboard> 
       </VisualState> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 

     <ContentControl x:Name="contentControl" Foreground="{TemplateBinding Foreground}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/> 
    </Grid> 
</ControlTemplate> 

的問題是,ListBoxItem類已正確放置選擇狀態在自己的視覺狀態組,獨立於常見的狀態,如鼠標。這意味着ListBoxItem可以處於選定狀態和鼠標懸停狀態。

如果選擇了ListBoxItem並正確顯示爲藍色,將鼠標移到黑色上會將其恢復爲黑色,因爲它會轉換回正常狀態。

有沒有什麼方法可以幫助我處理這個問題,而無需通過子類化ListBoxItem並添加我自己的自定義狀態?我讀過的所有內容都表明這不可能,但對我來說似乎有點可笑。我錯過了什麼?

回答

3

你基本上要求Foreground在同一時間是黑色和藍色。現在,這是不可能的。如果單個狀態具有優先級,則可以解決衝突,如MouseOver> Selected> Normal> Unselected。但它會給已經複雜的視覺狀態管理器帶來不必要的複雜性。通常情況下,這種情況通過添加新元素並在衝突狀態組之一中爲該元素的屬性設置動畫來解決。

+0

+1優先級是什麼,我問了 - 不是魔術;) – 2010-09-11 21:08:33