在ControlTemplate中改变WPF TextBox的前景色

4
我有一些采用控件模板的样式化文本框,可以根据视觉状态(例如鼠标悬停、禁用等)设置背景颜色。代码取自微软的TextBox Templates 页面。
我想要做的是,在不同的视觉状态下也改变前景(字体)颜色。例如,在鼠标悬停时,我希望使文字颜色更突出,在禁用时,我希望将其变灰。
我的xaml(已删除“正常”和“禁用”的VisualState标记以及Border的几个<Border.Blah>子项):
<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>

我最初尝试在故事板标签中添加一个新的<ColorAnimationUsingKeyFrames>来改变前景色,代码如下:

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

但是这没有效果——文本颜色保持不变。
我认为这是由于位于ControlTemplate顶部的,因此我尝试将标签设置为ControlTemplate的子元素。但Visual Studio并不接受。(找不到Foreground类型。请验证您没有丢失程序集引用,并且所有引用的程序集都已构建。
我在SO上看了一下,似乎有关于通过模板绑定设置的属性不能更改,但在我的情况下,我正试图在模板中进行更改。
那么,如何使用可视状态更改控件模板中文本框的前景(字体)颜色?
1个回答

0

看起来我们只能通过更改文本框前景色来更改滚动查看器的前景色。为此,您可以使用触发器:

 <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground">
                <Setter.Value>
                    <SolidColorBrush Color="{StaticResource ControlDisabledForeground}"/>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="Foreground">
                <Setter.Value>
                    <SolidColorBrush Color="{StaticResource ControlReadOnlyForeground}"/>
                </Setter.Value>
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

您可以在此处查看完整代码: https://gist.github.com/Javad-Amiry2/5897049

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接