Reputation: 13557
I working on a XAML style for my controls. The code below is for setting the color of a stackpanel. It works fine but there is something else I want to do. If the trigger is activated I want to set the font color for all child items inside the stackpanel.
At the moment I only have textblocks inside the stackpanel and I know I could easily create a separate style for a textbock. But if this style is triggered it will only affect ONE and not ALL textblocks. But I want to change all items inside the stackpanel as soon as I got a mouseover trigger for the panel.
Is this even possible in XAML or do I have to code a regular event?
<Style x:Key="XStack" TargetType="StackPanel">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="SkyBlue" Offset="6"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<!-- Trigger-->
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True" >
<Setter Property="Background" Value="SkyBlue"/>
</Trigger>
</Style.Triggers>
</Style>
Upvotes: 12
Views: 22840
Reputation: 136613
Like Olle said, you can set the attached property TextElement.Foreground
to a custom value. For a child control/UI node in the visual tree, if the property is not set, WPF will walk up the UI hierarchy till it finds a value and use it. This means that all child controls can share a property value defined at the parent level.
This should work for all TextBlocks... however if your StackPanel contained a TextBox, its text color wouldn't be affected. It uses the Foreground property from the Control base class... So be sure to test it out with all possible child element types.
Upvotes: 3
Reputation: 336
Add this to your trigger:
<Setter Property="TextElement.Foreground" Value="Blue"></Setter>
Upvotes: 16