XAML - HyperlinkBut​​ton的VisualGroupState(什么错了?)

时间:2011-10-03 17:08:17

标签: wpf silverlight xaml microsoft-metro visualstatemanager

试图弄清楚我做错了什么(第一次玩视觉状态)。有人能指出我的问题吗?该应用程序非常失败,除此之外别无他法。

继承人xaml

<Style TargetType="HyperlinkButton">
    <Setter Property="Foreground" Value="{StaticResource HyperlinkTextBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="HyperlinkButton">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">
                            <Storyboard>
                                <ColorAnimation BeginTime="0" Duration="0.5" 
                                                Storyboard.TargetName="content"
                                                Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                                To="Red" />
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="MouseOver">
                            <Storyboard>
                                <ColorAnimation BeginTime="0" Duration="0.5"
                                                Storyboard.TargetName="content"
                                                Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                                To="White" />
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Grid>
                    <ContentPresenter x:Name="content" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该纠正3个地方:

1)VisualStateManager.VisualStateGroups代码必须位于根控件内,如Grid而不是ControlTemplate

2)ContentPresenter类没有Foreground属性。但是这个属性存在于ContentControl类中。替换此控件后,为属性ContentForeground添加显式绑定。

3)Duration属性的值应以秒为单位。虽然您可以使用表达式Duration="1",这意味着1天,但值0.5会使应用程序崩溃。半秒钟看起来像0:0:0.5

这是固定的风格:

    <Style TargetType="HyperlinkButton">
        <Setter Property="Foreground" Value="{StaticResource HyperlinkTextBrush}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="HyperlinkButton">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">
                                    <Storyboard>
                                        <ColorAnimation Duration="0:0:0.5"
                                            Storyboard.TargetName="content"
                                            Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                            To="Red" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Duration="0:0:0.5"
                                            Storyboard.TargetName="content"
                                            Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                            To="White" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentControl x:Name="content" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" 
                                        Foreground="{TemplateBinding Foreground}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>