【问题标题】:Change WPF TextBox Foreground color in ControlTemplate在 ControlTemplate 中更改 WPF 文本框的前景色
【发布时间】: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>

但这没有任何效果 - 文本颜色保持不变。

我认为这是由于 顶部的 造成的,因此我尝试将 标签设置为 ControlTemplate 的子标签。 Visual Studio 没有这些。 (未找到类型 Foreground。确认您没有丢失程序集引用并且所有引用的程序集都已构建。

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

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

【问题讨论】:

    标签: wpf controltemplate foreground


    【解决方案1】:

    看来我们只能通过改变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

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 2011-04-27
      • 2013-01-15
      • 2012-06-06
      • 2018-08-08
      • 2013-07-06
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多