![](/img/trans.png)
[英]Mutually Exclusive comboboxes that binds to same data source - MVVM implementation
[英]Animating same property from mutually exclusive 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
可以同時處於selected狀態和鼠標懸停狀態。
如果選擇了ListBoxItem
並正確顯示為藍色,則將鼠標懸停在其上方會將其還原為黑色,因為它會轉換回正常狀態。
我有什么辦法可以解決這個問題,而不必求助於ListBoxItem
子類化和添加自己的自定義狀態? 我讀過的所有內容都表明這是不可能的,但似乎限制了我。 我想念什么?
您基本上要求前台同時為黑色和藍色。 現在那是不可能的。 如果單個狀態具有優先權,則可以解決此沖突,例如MouseOver> Selected> Normal> Unselected。 但這會給已經很復雜的視覺狀態管理器帶來不必要的復雜性。 通常情況下,可以通過添加新元素並在沖突狀態組之一中對該元素的動畫設置動畫來解決這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.