1

我的 Silverlight 项目中有一个自定义按钮样式,并且想要设置按钮中文本的前景(和其他属性)。但我的想法是ContentPresenter在样式中使用 a 。这样,我仍然可以在 Button 中放置我想要的任何内容。

但是如果有文本作为内容,我希望能够设置某些属性,如 Foreground、FontFamily、FontSize 等。我还想在悬停时更改这些属性等。

这是我的风格(简化):

<Style x:Key="ButtonStyle" TargetType="Button">
    <!-- There are other properties here, but I left them out for brevity -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="ButtonBorder" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Padding="{TemplateBinding Padding}">
                    <ContentPresenter x:Name="ButtonContent" 
                                      Content="{TemplateBinding Content}" 
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                    </ContentPresenter>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <!-- I would like to change the Foreground here -->
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我找到的所有信息,告诉我添加TextBlock.Foreground="..."到 ContentPresenter,即:

                    <ContentPresenter x:Name="ButtonContent" 
                                      Content="{TemplateBinding Content}" 
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      TextBlock.Foreground="{TemplateBinding Foreground}">

                    </ContentPresenter>

但是,这对我不起作用。我收到一条错误消息,指出可附加属性 Foreground 不适用于 TextBlock 类型。这是因为该解决方案仅适用于 WPF,然后我如何在 Silverlight 中设置此属性?

我知道我可以通过<Setter>标签设置前景,但是如何在鼠标悬停时更改它?我想设置 ContentPresenter 的 Foreground,以便在 MouseOver VisualState 中,我可以更改它。

4

2 回答 2

1

(对不起,我的英语不好)

您可以在 Button 上设置属性,它应该可以工作:

<Button Style="{StaticResource ButtonStyle}" Content="Test" Foreground="Red" FontFamily="Georgia" FontSize="25"/>

或者,您可以为文本按钮创建样式:

<Style x:Key="ButtonTextStyle" TargetType="Button" BasedOn="{StaticResource ButtonStyle}">
    <Setter Property="Foreground" Value="Red"/>
</Style>

并使用它:

<Button Style="{StaticResource ButtonTextStyle}" Content="Test"/>

编辑:

尝试将 ContentControl 放在 ContentPresenter 的位置 - 这样您将拥有 ForegroundProperty 和 VisualState,在 StoryBoard 内:

<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ButtonContent">
    <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
</ObjectAnimationUsingKeyFrames>
于 2011-12-03T15:40:35.117 回答
0

看一下用于 Button @ http://msdn.microsoft.com/en-us/library/cc278069(v=vs.95).aspx的基线控制模板,我建议您使用该控件然后进行调整根据您的要求将鼠标悬停。因此,为了让您将鼠标悬停在红色上,我会执行以下操作

 <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="Red"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="Red"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" To="Red"/>
                                    </Storyboard>
                                </VisualState>
于 2011-12-03T16:31:43.977 回答