4

我有样式化的文本框,它使用控制模板根据视觉状态(鼠标悬停、禁用等)设置背景颜色。代码取自 MS 的TextBox 模板页面。

我想要做的也是根据视觉状态改变前景(字体)颜色。例如,在鼠标悬停时,我想让文本颜色突出,而在禁用时,我想让它变灰

我的 xaml(我已经删除了 'Normal' 和 'Disabled' 的 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>

我首先尝试在 Storyboard 标记中添加一个新的 <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> 顶部的 <Border> 造成的,因此我尝试将 <Foreground ...> 标记设置为 ControlTemplate 的子项。Visual Studio 没有这些。(未找到类型 Foreground。验证您没有丢失程序集引用并且所有引用的程序集都已构建。

我已经查看了 SO,似乎与模板绑定设置​​的无法更改的属性有关,但在我的情况下,它在我试图进行更改的模板中。

那么,如何使用视觉状态更改控件模板中文本框的前景(字体)颜色?

4

1 回答 1

0

看来我们只能通过改变TextBox Foreground来改变ScrollViewer Foreground。为此,您可以使用触发器:

 <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

于 2019-01-09T08:22:08.143 回答