Change background color of WPF TextBox in ControlTemplate

I have Styled textboxes that use a control pattern to set the background color based on the visual state (Mouseover, Disabled, etc.). The code was taken from MS ' TextBox Templates page .

What I want to do is change the foreground (font) color depending on the visual state. For example, in Mouseover I want to highlight the text color, and on disabled, I want to gray it

My xaml (I removed the VisualState tags for "Normal" and "Disabled" and a couple of Border children) <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>

      

I first tried adding a new <ColorAnimationUsingKeyFrames> inside the Storyboard tag to change the Foreground so that it looks like this:

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

      

but this did not affect - the text color remains the same.

I figured this was due to the <Border> at the top of the <ControlTemplate>, so I tried to set the <Foreground ...> tag as a child of the ControlTemplate. This was not the case in Visual Studio. (The Foreground type was not found. Make sure you have not missed the assembly reference and that all referenced assemblies have been built.)

I have looked at SO and it seems to have something to do with the properties set by the template binding that cannot be changed , but in my case this is in the template I am trying to inject.

So how do I change the Foreground (font) color of a textbox in a control template using visual states?

+4


source to share


1 answer


It seems that we can only change the ScrollViewer Foreground by changing the TextBox Foreground. You can use Trigger for this:

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

      



You can see the complete code here: https://gist.github.com/Javad-Amiry2/5897049

0


source







All Articles