Is there a way to change the Foreground
of a custom TextBox
in xaml with triggers or visualstates without change the local main Foreground property?
Here is the xaml style of a generic custom TextBox with randomly chosen colors:
<Style TargetType="{x:Type local:CustomTextBox}">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomTextBox}">
<Border x:Name="PART_Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1">
<ScrollViewer x:Name="PART_ContentHost"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
Focusable="False"/>
</Border>
<ControlTemplate.Triggers>
<!-- Can be IsMouseOver, IsFocused, etc... -->
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_Border"
Property="Background" Value="Green"/>
<Setter TargetName="PART_Border"
Property="BorderBrush" Value="DarkGreen"/>
<!-- The only method I know that works is this one
that changes the local property -->
<Setter Property="Foreground" Value="Yellow"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've tried to use these lines in trigger but everyone fails (does nothing):
<Setter TargetName="PART_ContentHost" Property="Foreground" Value="Yellow"/>
<Setter TargetName="PART_ContentHost" Property="TextBlock.Foreground" Value="Yellow"/>
<Setter TargetName="PART_ContentHost" Property="TextElement.Foreground" Value="Yellow"/>
<Setter TargetName="PART_Border" Property="TextBlock.Foreground" Value="Yellow"/>
<Setter TargetName="PART_Border" Property="TextElement.Foreground" Value="Yellow"/>
In Buttons the Foreground color can be changed by changing TextBlock.Foreground
of the parent element (e.g. TextBlock.Foreground
of PART_Border
), but this not works with TextBoxes.
Changing the local property as this line does...
<Setter Property="Foreground" Value="Yellow"/>
...has the problem that if I change later the main Foreground property (from Black to Gray for example), the trigger cannot change it anymore to Yellow, for example with this:
<local:CustomTextBox ... Foreground="Gray"/>
VisualStates cannot interact even with the main Foreground property.
So is there another way I don't know to accomplish this in xaml or is it a limitation of wpf?
You could animate the property using a
Storyboard
. This will take effect over local values: