0

试图找出我做错了什么(第一次玩视觉状态)。谁能指出我的问题?该应用程序正在不正常地崩溃,除此之外没有其他帮助。

这是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>

谢谢!

4

1 回答 1

1

有 3 个地方需要更正:

1)VisualStateManager.VisualStateGroups标签必须像 a 一样位于根控件内部,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>
于 2011-10-03T20:01:04.183 回答