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

发布于 2025-01-05 15:37:59 字数 922 浏览 1 评论 0原文

我正在尝试使用 XAML 在 Silverlight 4 中将超链接按钮的文本设为粗体。我尝试修改 HyperlinkBut​​ton 模板,但收到 InvalidOperationException: TargetProperty "(Content).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>

I am trying to make a the text of a hyperlinkbutton bold in Silverlight 4 using XAML. I have tried modifying the HyperlinkButton template, but I get an InvalidOperationException: TargetProperty "(Content).FontWeight" can not be resolved.

What am I doing wrong?

....
<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2025-01-12 15:37:59

我为一个标签创建了两个故事板,然后使用一种行为将它们设置为动画。故事板如下所示。

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

I created two storyboards for a label and then animated them using a behavior. The storyboard looked like below.

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

Edit: I thought you were asking about animating the font weight in general. In case of a hyperlink button, you could edit the content template and insert a textblock/label inside it and animate that.

    <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>
伤感在游骋 2025-01-12 15:37:59

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>

ContentPresenter doesn't have FontWeight property. You can change it to ContentControl and apply animation to it.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文