我的 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 中,我可以更改它。