microsoft-ui-xaml: WinUI3 Button XAML Template does not respect Foreground property after mouse passes over
Describe the bug
In WInUI 3, create a Button with:
<Button Name="Undo1" Content="Undo." Background ="DarkSlateBlue" Foreground ="White"/>
On starting the app, the colors are correct, but after moving the mouse over the button, the text color will remain black rather that white as specified with Foreground property.

Steps to reproduce the bug
as above.
Note that the key issue is that the default template for Button (inside generic.xaml) does not have a templatebinding for Foreground, but includes Background. My reference for the template is: C:\Users.….nuget\packages\microsoft.windowsappsdk\1.0.3\lib\net5.0-windows10.0.18362.0\Microsoft.WinUI\Themes
The template contains:
<Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
It should contain:
<Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
<Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
Expected behavior
Foreground color property should be reset to original color after the mouse has left the button zone. It will do that if the generic template is updated as suggested
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.0.3
Windows app type
- UWP
- Win32
Device form factor
Desktop
Windows version
Windows 10 (21H2): Build 19044
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19 (5 by maintainers)
🎉
Microsoft.WindowsAppSDK.1.2.221209.1has been released which fixes this issue.Handy links:
In this particular case, there is a template bind to the foreground property for the control. The foreground property is inherited by the ContentPresenter in the template. The PointerOver visual state is writing the foreground property and when going back to normal, the inhertited property is not going back to its original value. A workaround would be to explicitly set the foreground property on the ContentPresenter to a template binding instead of it getting inherited. That unfortunately needs a re-template. Testing a fix to WinUI3’s button template.
@michael-hawker There are other cases where the properties really need to be template bound instead of pointing to theme resources like you mention in your linked bug.
Thank you very much for posting that, @OmarGGil! I am using 1.2.230313.1 and was still having the problem. Your mod fixed it for me.
After a few days looking for a workaround i found that i was overriding the DefaultButtonStyle and i just needed to use BasedOn=“DefaultButtonStyle” at all my button styles, so that there’s no property missing anymore.
Changed this:
<Style TargetType="Button" x:Key="btnCoppelSecondary" > <Setter Property="Background" Value="White"/> <Setter Property="Foreground" Value="{StaticResource Highlight}"/> <Setter Property="BorderBrush" Value="{StaticResource Highlight}"/> <Setter Property="Margin" Value="10,0,10,0"/> <Setter Property="CornerRadius" Value="16"/> <Setter Property="FontSize" Value="16"/> <Setter Property="FontWeight" Value="SemiBold"/> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="MinWidth" Value="200"/> </Style>For this:
<Style TargetType="Button" x:Key="btnCoppelSecondary" BasedOn="{StaticResource DefaultButtonStyle}"> <Setter Property="Background" Value="White"/> <Setter Property="Foreground" Value="{StaticResource Highlight}"/> <Setter Property="BorderBrush" Value="{StaticResource Highlight}"/> <Setter Property="Margin" Value="10,0,10,0"/> <Setter Property="CornerRadius" Value="16"/> <Setter Property="FontSize" Value="16"/> <Setter Property="FontWeight" Value="SemiBold"/> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="MinWidth" Value="200"/> </Style>@ranjeshj @kmahone @llongley who were the latest to work on this and can provide a status update.
@Jimbo99 does this post not provide enough guidance on how to fix this issue in your app: https://nicksnettravels.builttoroam.com/fixing-broken-defaultbuttonstyle/