在ControlTemplate中更改WPF TextBox前景色

时间:2013-02-14 07:12:49

标签: wpf controltemplate foreground

我有Styled文本框,它使用控制模板根据视觉状态设置背景颜色(Mouseover,Disabled等)。代码来自MS“TextBox Templates页面。

我想要做的是根据视觉状态更改前景(字体)颜色。例如,在Mouseover上,我想让文本颜色脱颖而出,而在禁用时,我想要将它变灰了

我的xaml(我删除了'Normal'和'Disabled'的VisualState标签以及一些< Border.Blah> 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>

我首先尝试添加一个新的&lt; ColorAnimationUsingKeyFrames&gt;在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>

但没有效果 - 文字颜色保持不变。

我认为这是由于&lt; Border&gt;在&lt; ControlTemplate&gt;的顶部,所以我尝试设置&lt; Foreground ...&gt;标记为ControlTemplate的子项。 Visual Studio没有这个。 (未找到类型Foreground。确认您没有错过程序集引用,并且已构建所有引用的程序集。

我已经查看了SO,似乎与properties that are set by template binding that cannot be changed有关,但在我的情况下,我正在尝试进行更改。

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

1 个答案:

答案 0 :(得分:0)

似乎我们只能通过更改TextBox前景来更改ScrollViewer前景。为此,您可以使用触发器:

 <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