在MouseOver上更改超链接按钮的字体粗细

时间:2012-02-15 13:41:51

标签: silverlight hyperlink mouseover

我正在尝试使用XAML在Silverlight 4中创建一个超链接按钮粗体的文本。我尝试修改HyperlinkBut​​ton模板,但是我得到了一个InvalidOperationException:TargetProperty“(内容).FontWeight”无法解析。

我做错了什么?

....
<ControlTemplate TargetType="HyperlinkButton">
      <Grid x:Name="ButtonGrid" Cursor="{TemplateBinding Cursor}">
        <VisualStateManager.VisualStateGroups>
          <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal"/>
            <VisualState x:Name="MouseOver">
              <Storyboard>
               <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="(Content).FontWeight" Duration="0">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Bold" />
                 </ObjectAnimationUsingKeyFrames>
               </Storyboard>
...
</ControlTemplate>

2 个答案:

答案 0 :(得分:2)

我为标签创建了两个故事板,然后使用行为为它们制作动画。故事板如下所示。

      <ObjectAnimationUsingKeyFrames Storyboard.TargetName="label" Storyboard.TargetProperty="(Control.FontWeight)">
         <DiscreteObjectKeyFrame KeyTime="0">
            <DiscreteObjectKeyFrame.Value>
                <FontWeight>Normal</FontWeight>
            </DiscreteObjectKeyFrame.Value>
         </DiscreteObjectKeyFrame>
         <DiscreteObjectKeyFrame KeyTime="0:0:1">
            <DiscreteObjectKeyFrame.Value>
                <FontWeight>Bold</FontWeight>
            </DiscreteObjectKeyFrame.Value>
         </DiscreteObjectKeyFrame>
       </ObjectAnimationUsingKeyFrames>

编辑:我以为你一直在询问动画字体粗细。如果是超链接按钮,您可以编辑内容模板并在其中插入文本块/标签并为其设置动画。

    <HyperlinkButton Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="100" >
        <HyperlinkButton.Resources>
            <Style x:Key="HyperlinkButtonStyle1" TargetType="HyperlinkButton">
                <Setter Property="Foreground" Value="#FF73A9D8"/>
                <Setter Property="Padding" Value="2,0,2,0"/>
                <Setter Property="Cursor" Value="Hand"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Top"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="HyperlinkButton">
                            <Grid Background="{TemplateBinding Background}" Cursor="{TemplateBinding Cursor}">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="MouseOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="UnderlineTextBlock">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Collapsed</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Pressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="UnderlineTextBlock">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Visible</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOverlay">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Visible</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="FocusStates">
                                        <VisualState x:Name="Focused">
                                            <Storyboard>
                                                <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement"/>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Unfocused"/>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <TextBlock x:Name="UnderlineTextBlock" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Text="{TemplateBinding Content}" TextDecorations="Underline" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                <TextBlock x:Name="DisabledOverlay" Foreground="#FFAAAAAA" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Text="{TemplateBinding Content}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Canvas.ZIndex="1"/>
                                <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Opacity="0" Stroke="#FF6DBDD1" StrokeThickness="1"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <DataTemplate x:Key="DataTemplate1">
                <Grid>
                    <!--Make your adimation related changes to this textblock-->
                    <TextBlock Text="{Binding}" />
                </Grid>
            </DataTemplate>
        </HyperlinkButton.Resources>
        <HyperlinkButton.ContentTemplate>
            <StaticResource ResourceKey="DataTemplate1"/>
        </HyperlinkButton.ContentTemplate>
        <HyperlinkButton.Style>
            <StaticResource ResourceKey="HyperlinkButtonStyle1"/>
        </HyperlinkButton.Style>
        <HyperlinkButton.Content>
            test
        </HyperlinkButton.Content>
    </HyperlinkButton>

答案 1 :(得分:1)

ContentPresenter没有FontWeight属性。您可以将其更改为ContentControl并对其应用动画。

<ControlTemplate TargetType="HyperlinkButton">
  <Grid x:Name="ButtonGrid" Cursor="{TemplateBinding Cursor}">
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal"/>
        <VisualState x:Name="MouseOver">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="FontWeight" Duration="0">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Bold" />
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
     ...
     <ContentControl x:Name="ContentPresenter" .../>
 </ControlTemplate>