I would like to trigger a change in a Button's template to complement the FocusVisualStyle effects. Basically I want the text 'foo' in the snippet below to turn red if and only if FocusVisualStyle is visible:
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<ContentPresenter/>
<TextBlock x:Name="TxtFoo" Text="foo" Foreground="Black"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="Foreground" TargetName="TxtFoo" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Please ignore the fact that the example is idiotic, the actual code is more useful (it modifies a Path).
This works on keyboard navigation, but has an issue: the text turns red also when the button is pressed (FocusVisualStyle is not displayed in that case). Using an eventTrigger on GotKeyboardFocus/LostKeyboardFocus yields the same result.
Looking at the framework's source code I don't see anything special: KeyboardNavigation.ShowFocusVisual()
is called by FrameworkElement.OnGotKeyboardFocus()
as expected. However, obviously there must be something else going on because not every gotKeyboardFocus events cause FocusVisualStyle to show.
What property/event should I target if I want to be "synchronized" with FocusVisualStyle?