从互斥的VisualStateGroups中动画相同的属性

时间:2010-09-11 20:40:52

标签: silverlight silverlight-4.0 visualstatemanager

我的问题很简单:它甚至可能吗?

假设我要为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并添加自己的自定义状态的情况下处理这个问题?我读过的所有内容都表明这是不可能的,但这似乎是对我的无限限制。我错过了什么?

1 个答案:

答案 0 :(得分:3)

你基本上要求Foreground同时为黑色和蓝色。现在这是不可能的。如果个别州具有优先权,则可以解决此冲突,例如MouseOver&gt;选择&gt;正常&gt;未选中。但它会给已经很复杂的视觉状态管理器带来不必要的复杂性。通常,通过添加新元素并在其中一个冲突状态组中为该元素的属性设置动画来解决此问题。

相关问题