【发布时间】:2013-01-29 22:40:05
【问题描述】:
我有样式文本框,它使用控制模板根据视觉状态(鼠标悬停、禁用等)设置背景颜色。代码取自 MS 的 TextBox Templates 页面。
我想要做的也是根据视觉状态更改前景(字体)颜色。例如,在鼠标悬停时,我想让文本颜色突出,而在禁用时,我想让它变灰
我的 xaml(我已经删除了 'Normal' 和 'Disabled' 的 VisualState 标签以及 Border 的几个
<Color x:Key="EditableControlHiLightColor">Ivory</Color>
<Color x:Key="EditableControlHiLightTextColor">Pink</Color>
<Style TargetType="{x:Type TextBox}">
<Setter Property="MinWidth" Value="100" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border Name="Border"
CornerRadius="4"
Padding="2"
BorderThickness="1">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="MouseOver" >
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(TextBox.Background).Color">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource EditableControlHiLightColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ScrollViewer Margin="0" x:Name="PART_ContentHost" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我首先尝试在 Storyboard 标记内添加一个新的
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(TextBox.Background).Color">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource EditableControlHiLightColor}" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(TextBox.Foreground).Color">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource EditableControlHiLightTextColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
但这没有任何效果 - 文本颜色保持不变。
我认为这是由于
我查看了 SO,似乎与 properties that are set by template binding that cannot be changed 有关,但在我的情况下,我试图在模板中进行更改。
那么,如何使用视觉状态更改控件模板中文本框的前景(字体)颜色?
【问题讨论】:
标签: wpf controltemplate foreground